jeudi 6 août 2020

Get random operators and integers

im doing an assignment which requires me to create 3 classes.

  • 1 with getting 2 random integers,
  • 1 with getting random operators such as + - * / which should be done so in char method
  • the last one to check if the simple mathematics answer is correct using booleans via user Scanner input.

i need a little help here as im not sure if i've done the random operators method correctly and am really lost at how i should tabulate both random int and operators together in one class.

here's the code i've done so far:

    class Test
{   
    private static Random rand; // Instance of random class
    static Scanner input = new Scanner (System.in);
    
    
    public static double getTwoIntegers (MyInteger m, MyInteger n)
    {
        return Math.random () * 10.0;
    }
    
    public static char getOperator (int x, int y)
    {
        char operator;
        int answer;
        
        switch (rand.nextInt(4))
          {
            case 0: operator = '+';
                    answer = x + y;
                    break;
                    
            case 1: operator = '-';
                    answer = x - y;;
                    break;
                    
            case 2: operator = '*';
                    answer = x * y;;
                    break;
                    
            case 3: operator = '/';
                    answer = x / y;;
                    break;
                    
            default: operator = '?';
          }
        return operator;
    }
    
    public static boolean checkAnswer (int x, int y, char oper, int answer)
    {
          void checkAnswer() {
               do {
                   System.out.println("What is the answer? ");
                   answer = myScanner.nextLine();
                    
                            
                   if (answer.equals(str)) {
                       System.out.println("Correct!"); 
                   } else {
                       System.out.println("Nope! Try again. ");
                   }
               } while ( !answer.equals(str));
           }
    }



Aucun commentaire:

Enregistrer un commentaire