vendredi 26 mars 2021

How to assign random numbers to 2 variables, and then assign new random numbers to those 2 variables when needed?

I need to write a program (using inheritance) that uses a random number generator to produce 2 random numbers, present the two numbers as an addition question, and then when the question is answered, produce a new question that uses new random numbers. So far, I have the following code in 3 separate java files:

public int numberGenerator()
   {
       int num = 0;
       Random ranNum = new Random();
       ArrayList<Integer> list = new ArrayList<Integer>();
       
       for (int i = 1; i < 20; i++)
       {
           num = ranNum.nextInt(10);
           list.add(num);
       }
       
       Collections.shuffle(list);
       for (int i = 0; i<20; i++)
       {
           list.get(num);
       }
       
       return num;
   }
int rand1 = numberGenerator();
int rand2 = numberGenerator();
int sum = rand1 + rand2;
String sumCompare = String.valueOf(sum);

And I have this code to present the question:

while (problem.getSum() < 10)
            {
                problem.setText("What is " + problem.getRand1() + " + " + problem.getRand2()
                + " ?");
                problem.setAnswer(problem.getSumCompare());
                presentQuestion(problem);
            }  

Let's say the random numbers it spits out is 2 and 3. It will ask me, "What is 2+3?" And then I'll answer it correctly, and it asks me another question, but it will again ask me "What is 2+3?" I need the code to ask me a different addition question, and not use the same numbers over and over again. I apologize in advance if I am missing information and please let me know if more needs to be known, I tried to include as much as I could!




Aucun commentaire:

Enregistrer un commentaire