rk9
rk9

Reputation: 33

How to get a new random number at the start of iteration of a loop Java

I'm fairly new to coding and as a project I'm trying to create a question and answer program for practical use. In theory, what this program should do is ask you for how many questions you want then give you a random selection of questions and the corresponding answer to the questions. Through each iteration of the loop it should give you a new question and answer but for some reason it repeatedly gives the same question and answer. Any help would be highly appreciated.

Random rand = new Random();
    int int_random = rand.nextInt(6);
    int storage = int_random;

    String[] questionV = {"Question 1", "Question 2", "Question 3", "Question 4", "Question 5"};
    String[] answerV = {"Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5",};

   public void question_answerV() {

        System.out.println("How many questions would you like? (1-5) ");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                System.out.println(questionV[int_random]);
                System.out.println("Please press any number for the answer");
                Scanner scanner1 = new Scanner(System.in);
                int choiceA = scanner1.nextInt();
                System.out.println(answerV[storage]);
                break;

            case 2:
                for (int i = 0; i < 2 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 3:
                for (int i = 0; i < 3 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 4:
                for (int i = 0; i < 4 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 5:
                for (int i = 0; i < 5 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;
        }
    }

Upvotes: 0

Views: 65

Answers (1)

WJS
WJS

Reputation: 40057

Some observations.

  • As already mentioned, use only one Scanner instance.
  • You are also repeating code. Just use the choice (that you switch on) as the loop terminating value
  • look at the following code:
    • int_random never changes.
    • you prompt for choiceA but never use it
    • since you initialized storage to int_random at the beginning, you will always get the same q and a.
for (int i = 0; i < 4 ; i++ ) {
  System.out.println(questionV[int_random]);
  System.out.println("Please press any number for the answer");
  scanner1 = new Scanner(System.in);
  choiceA = scanner1.nextInt();
  System.out.println(answerV[storage]);
}

If (and it's not clear) you want to select questions and answers randomly.

  • initialize an ArrayList from 0 to n choice as appropriate.

  • Call Collections.shuffle(list) to randomize the list.

  • then just choose a question number from the list starting with 0, 1, 2... using list.get(). Use the obtained number for the question and answer. None will be repeated.

  • And I recommend a little defensive programming. Make certain that choice is within the bounds of the array of questions. If not, let the user know and reprompt. Otherwise you could get an ArrayIndexOutOfBoundsException.

Upvotes: 3

Related Questions