0

I have a project where I made an x + y = z or - , * operator generated function, but I do not get it how can I make another class where it will generate different answers and the user has to find the right one.

Exemple: 2*2 = z

A) 2

B) 5

C) 6

D) 4

Here's the code of what I did already:

public class Equation {
    int x,y,z;

        public Equation() {
        Random r = new Random();
         x = r.nextInt(50) + 1;
         y = r.nextInt(50) + 1;
         z = 0;
        char operator ='?';         

        switch (r.nextInt(3)){
        case 0: operator = '+';
                z = x+y;
                break;
        case 1: operator = '-';
                z = x-y;;
                break;
        case 2: operator = '*';
                z = x*y;;
                break;
        default: operator = '?';
      }

      System.out.print(x);
      System.out.print(" ");
      System.out.print(operator);
      System.out.print(" ");
      System.out.print(y);
      System.out.print(" = ");
      System.out.println(z);


    }
    public static void main(String[] args) {
        Equation eq= new Equation();
        String param = null;




    }

}

Yet I do not ask for a code already made but indications.

Thank you.

BetaDev
  • 4,516
  • 3
  • 21
  • 47
Jeffy
  • 11
  • 3

1 Answers1

0

You ask, "how I can make another class where it will generate different answers and the user has to find the right one." One way to do that is to take the correct value, z, and generate random values which are different, and then output all the values, correct and incorrect.

Something like:

incorrect = r.nextInt(2*z);

which generates a value in the range from 0 to 2*z. You will need to ensure that the incorrect values are all different from each other, and all different from z.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • ok I see just a little question ( I am kinda new to programming world so I don't know well) umm I will have this in a separate class than the answer one so to import my x,y,z do I do something like a constructor to call the fonction? – Jeffy Feb 19 '18 at 17:42
  • Yes, you can create another class, and supply z in the constructor for that class. That is one way to do it; there are other ways, but that way will work. – Robert Dodier Feb 19 '18 at 17:49
  • this is what I did if you wanna see: – Jeffy Feb 19 '18 at 18:20
  • `public class Reponse { Equation equ = new Equation(); int a, b, c, d; char operator = '!'; public Reponse() { Random r = new Random(); switch (r.nextInt(4)) { case 0: a = r.nextInt(2 * equ.z); break; case 1: b = r.nextInt(2 * equ.z); break; case 2: c = r.nextInt(2 * equ.z); break; case 3: d = equ.z; break; default: operator = '?'; } } } ` – Jeffy Feb 19 '18 at 18:21
  • I think that is incorrect. Look at it this way: you need to generate 3 values which are not equal to z, and not equal to each other. One way to do that is to make a loop which generates random values. If the generated value is equal to z or equal to a previous value, then you must generate another one. Otherwise, the value is different from z and different from all previous values, and you can store it. For example, you can store it by appending the generated value to a list. The first value on the list is z. When you have generated 3 more values, then make a random permutation of the list. – Robert Dodier Feb 19 '18 at 18:36