Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.program.*;
- import java.util.Random;
- import java.awt.Font;
- public class MathQuiz extends ConsoleProgram
- {
- final int SIZE = 5;
- final int NUM_TRIES = 3;
- int[] nums = new int[2*SIZE];
- public void run()
- {
- GenNums();
- }
- public void GenNums()
- {
- Random rnd = new Random();
- boolean operator = rnd.nextBoolean();
- for (int i=0;i<2*SIZE;i++)
- {
- nums[i] = rnd.nextInt(21);
- print(nums[i]+" \n");
- }
- for (int i=0;i<2*SIZE;i=i+2)
- {
- int counter = NUM_TRIES;
- boolean correct = false;
- while(counter-- > 0 && !correct){
- int num1;
- int ans;
- if(nums[i+1]>nums[i])
- {
- num1 = nums[i+1]-nums[i];
- ans = readInt(num1+"+"+nums[i]+"= ");
- }
- else
- {
- num1 = nums[i]-nums[i+1];
- ans = readInt(nums[i]+"-"+num1+"= ");
- }
- if(ans == nums[i+1])
- {
- println(GenResponse());
- correct = true;
- }
- else
- {
- ans = readInt("That's incorrect - try a different answer: ");
- }
- }
- }
- }
- public String GenResponse()
- {
- String[] response = new String[5];
- response[0]=("Correct!");
- response[1]=("That's the answer!");
- response[3]=("You got it!");
- response[4]=("Right!");
- Random rnd = new Random();
- return response[rnd.nextInt(5)];
- }
- public static void main(String[] args)
- {
- new MathQuiz().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement