Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- // 1 - define variables
- TextView tvQuestion, tvScore;
- RadioButton rbtn1, rbtn2, rbtn3;
- RadioGroup rgOptions;
- Button btnCheck, btnStartAgain;
- Random rnd;
- int n1, n2, answer;
- int score;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 2 - bind each variable to a view from the layout
- tvQuestion = findViewById(R.id.tvQuestion);
- tvScore = findViewById(R.id.tvScore);
- rbtn1 = findViewById(R.id.rbtn1);
- rbtn2 = findViewById(R.id.rbtn2);
- rbtn3 = findViewById(R.id.rbtn3);
- rgOptions = findViewById(R.id.rgOptions);
- btnCheck = findViewById(R.id.btnCheck);
- btnStartAgain = findViewById(R.id.btnStartAgain);
- // 3 - set a listener for each view event
- btnCheck.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- checkAnswer();
- }
- });
- btnStartAgain.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- reStart();
- }
- });
- // 4 - select a new question and update ui
- rnd = new Random();
- reStart();
- }
- private void checkAnswer() {
- int userAnswer;
- switch (rgOptions.getCheckedRadioButtonId()) {
- case R.id.rbtn1:
- userAnswer = Integer.parseInt(rbtn1.getText().toString());
- break;
- case R.id.rbtn2:
- userAnswer = Integer.parseInt(rbtn2.getText().toString());
- break;
- case R.id.rbtn3:
- userAnswer = Integer.parseInt(rbtn3.getText().toString());
- break;
- default:
- userAnswer = -1;
- }
- if (userAnswer == -1) {
- Toast.makeText(MainActivity.this, "choose an answer first!", Toast.LENGTH_SHORT).show();
- } else {
- if (answer == userAnswer) {
- // update and show score
- score++;
- tvScore.setText("Score: " + score);
- Toast.makeText(MainActivity.this, "good work!", Toast.LENGTH_SHORT).show();
- // Show new question
- newQuestion();
- } else {
- // stop currrent game
- btnCheck.setEnabled(false);
- rbtn1.setEnabled(false);
- rbtn2.setEnabled(false);
- rbtn3.setEnabled(false);
- // display restart button
- btnStartAgain.setVisibility(View.VISIBLE);
- }
- }
- }
- private void reStart() {
- btnStartAgain.setVisibility(View.GONE);
- score = 0;
- tvScore.setText("Score: " + score);
- btnCheck.setEnabled(true);
- rbtn1.setEnabled(true);
- rbtn2.setEnabled(true);
- rbtn3.setEnabled(true);
- newQuestion();
- }
- private void newQuestion() {
- n1 = rnd.nextInt(50);
- n2 = rnd.nextInt(50);
- answer = n1 + n2;
- tvQuestion.setText(" " + n1 + " + " + n2 + " ");
- int opt1, opt2;
- do {
- opt1 = rnd.nextInt(100);
- } while (opt1 == answer);
- do {
- opt2 = rnd.nextInt(100);
- } while (opt2 == answer || opt2 == opt1);
- switch (rnd.nextInt(3)) {
- case 0:
- rbtn1.setText(answer + "");
- rbtn2.setText(opt1 + "");
- rbtn3.setText(opt2 + "");
- break;
- case 1:
- rbtn1.setText(opt1 + "");
- rbtn2.setText(answer + "");
- rbtn3.setText(opt2 + "");
- break;
- case 2:
- rbtn1.setText(opt1 + "");
- rbtn2.setText(opt2 + "");
- rbtn3.setText(answer + "");
- break;
- }
- // clear current selection
- rgOptions.clearCheck();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement