Facebook
From Chunky Coyote, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 178
  1. package com.example.mathchallenge;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. import java.util.Random;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.     int score = 0; //to keep track of the user's score
  13.     int num1, num2, answer; //variables to hold the first number, second number, and answer respectively
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity\_main);
  19.         newProblem(); //generate a new problem when the app starts
  20.     }
  21.  
  22.     public void checkAnswer(View view) {
  23.         EditText editText = findViewById(R.id.answer);
  24.         int userAnswer;
  25.         try {
  26.             userAnswer = Integer.parseInt(editText.getText().toString());
  27.         } catch (NumberFormatException e) {
  28.             editText.setError("Invalid answer!");
  29.             return;
  30.         }
  31.         if (userAnswer == answer) {
  32.             score++;
  33.             TextView textView = findViewById(R.id.score);
  34.             textView.setText("Score: " + score);
  35.             TextView message = findViewById(R.id.message);
  36.             message.setText("Correct!");
  37.         } else {
  38.             TextView message = findViewById(R.id.message);
  39.             message.setText("Sorry, the correct answer was " + answer + ".");
  40.         }
  41.     }
  42.  
  43.     public void playAgain(View view) {
  44.         newProblem();
  45.         score = 0;
  46.         TextView textView = findViewById(R.id.score);
  47.         textView.setText("Score: 0");
  48.     }
  49.  
  50.     private void newProblem() {
  51.         Random random = new Random();
  52.         int operator = random.nextInt(4); //generating a number to determine the operator
  53.         switch (operator) {
  54.             case 0: //addition
  55.                 num1 = random.nextInt(51); //generate a random number between 0 and 50
  56.                 num2 = random.nextInt(51); //generate another random number between 0 and 50
  57.                 answer = num1 + num2;
  58.                 break;
  59.             case 1: //subtraction
  60.                 num1 = random.nextInt(101);
  61.                 num2 = random.nextInt(num1+1); //generate a number between 0 and num1
  62.                 answer = num1 - num2;
  63.                 break;
  64.             case 2: //multiplication
  65.                 num1 = random.nextInt(13); //generate a random number between 0 and 12
  66.                 num2 = random.nextInt(13);
  67.                 answer = num1*num2;
  68.                 break;
  69.             case 3: //division
  70.                 num1 = random.nextInt(13) * random.nextInt(13); //multiply two random numbers between 0 and 12 to get num1
  71.                 num2 = random.nextInt(12) + 1; //generate a random number between 1 and 12 for num2
  72.                 answer = num1 / num2;
  73.                 break;
  74.         }
  75.  
  76.         TextView textView = findViewById(R.id.problem);
  77.         textView.setText(num1 + " ? " + num2 + " = ?");
  78.         TextView message = findViewById(R.id.message);
  79.         message.setText("");
  80.         EditText editText = findViewById(R.id.answer);
  81.         editText.setText("");
  82.     }
  83. }
  84.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Torrid Tamarin text 2 Years ago.