Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import acm.program.*;
  2. import java.util.Random;
  3. import java.awt.Font;
  4.  
  5. public class MathQuiz extends ConsoleProgram
  6. {
  7.     final int SIZE = 5;
  8.     final int NUM_TRIES = 3;
  9.     int[] nums = new int[2*SIZE];
  10.     public void run()
  11.     {
  12.         GenNums();
  13.     }
  14.     public void GenNums()
  15.     {
  16.         Random rnd = new Random();
  17.         boolean operator = rnd.nextBoolean();
  18.         for (int i=0;i<2*SIZE;i++)
  19.         {
  20.             nums[i] = rnd.nextInt(21);
  21.             print(nums[i]+" \n");
  22.         }
  23.        
  24.         for (int i=0;i<2*SIZE;i=i+2)
  25.             {
  26.                 int counter = NUM_TRIES;
  27.                 boolean correct = false;
  28.                 while(counter-- > 0 && !correct){
  29.                     int num1;
  30.                     int ans;
  31.                     if(nums[i+1]>nums[i])
  32.                     {
  33.                         num1 = nums[i+1]-nums[i];
  34.                        ans = readInt(num1+"+"+nums[i]+"= ");
  35.                     }
  36.                     else
  37.                     {
  38.                         num1 = nums[i]-nums[i+1];
  39.                         ans = readInt(nums[i]+"-"+num1+"= ");
  40.                     }
  41.                    
  42.                     if(ans == nums[i+1])
  43.                     {
  44.                         println(GenResponse());
  45.                         correct = true;
  46.                     }
  47.                     else
  48.                     {
  49.                         ans = readInt("That's incorrect - try a different answer: ");
  50.                     }
  51.                 }
  52.             }
  53.     }
  54.     public String GenResponse()
  55.     {
  56.         String[] response = new String[5];
  57.         response[0]=("Correct!");
  58.         response[1]=("That's the answer!");
  59.         response[3]=("You got it!");
  60.         response[4]=("Right!");
  61.         Random rnd = new Random();
  62.         return response[rnd.nextInt(5)];
  63.     }
  64.     public static void main(String[] args)
  65.     {
  66.         new MathQuiz().start();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement