0

Why does the second while loop while (numberOfTries < 2) cancel both while loops? It runs perfect if there is no incorrect answer. But let's say I select 4 problems to be made, and I am only on the first problem. I give the incorrect answer 2 times so the program should say Incorrect two times and then give me a new question because while (numberOfTries < 2) should force it to break from that loop. But it doesn't, it just quits the whole thing. I know it has to be a logic issue, so what am I missing?

import java.util.Random;
import java.util.Scanner;

public class Howthe {

    public static void main(String[] args) {
        // Open Scanner
        Scanner scan = new Scanner(System.in);

        // Ask user to choose number of problems to be made. 
        // Can only choose 4, 9, or 16
        System.out.print("Choose a number of problems to be made (4, 9, 16): ");
        int userChoiceOfProblems = scan.nextInt();

        // Ask user to choose a number between 0 and 12
        System.out.print("\nChoose a number between 0 and 12: ");
        int userNumberBetween0and12 = scan.nextInt();

        // Ask user to choose between add/sub or multiply/divide
        System.out.println("\nChoose to:"
                + "\n0: add/sub  your chosen number"
                + " and the randomly generated number: "
                + "\n1: multiply/divide your chosen number"
                + " and the randomly generated number: ");
        int userArithmeticChoice = scan.nextInt();

        int counter = 0;
        String equationString;
        int equationAnswer;
        int numberOfAnswersRight = 0; 
        int numberOfTries = 0;
        int userAnswerToQuestion;
        if (userArithmeticChoice == 0){
            while (counter < userChoiceOfProblems){
                // Create random number to decide if add or sub used.
                // add is equal to 0 and sub is equal to 1 
                Random rand = new Random();
                int randomNumberBetween0and1 = rand.nextInt(1) + 0;

                // Create random number that is multiplied by userNumberBetween0and12
                int randomNumberBetween0and12 = rand.nextInt(12) + 0;

                // Add and increase counter by 1
                if (randomNumberBetween0and1 == 0){
                    // If numberOfTries is more than 2, then display answer. 
                    while (numberOfTries < 2){
                        // Compute the right answer (addition). 
                        equationAnswer = userNumberBetween0and12 + randomNumberBetween0and12;
                        // Produce string of equation, then display string (addition).
                        equationString = userNumberBetween0and12 + " + "
                                + randomNumberBetween0and12;
                        System.out.println(equationString);
                        userAnswerToQuestion = scan.nextInt();

                        // If answer is right, increase numberOfAnswersRight.
                        if (userAnswerToQuestion == equationAnswer){
                            numberOfAnswersRight++;
                            System.out.println("Correct!");
                            break;
                        }
                        // If answer is wrong, continue loop and increase numberOfTries
                        else if (userAnswerToQuestion != equationAnswer){
                            numberOfTries++;
                            System.out.println("Incorrect");    
                        }   
                    } // end of while (numberOfTries < 2 && !quit)
                    counter++;
                }
            } System.out.println("Yout got " + numberOfAnswersRight + " problem(s) right!");
        }
    }
}
Trey
  • 63
  • 1
  • 9
  • 2
    What do you see if you step through the code in your debugger? – Peter Lawrey Oct 04 '16 at 18:20
  • To be honest, I've never used a debugger. – Trey Oct 04 '16 at 18:39
  • 2
    Unless you plan to never have a bug again, you should learn to use it. It's an essential tool which will help you solve problems in seconds instead of hours. – Peter Lawrey Oct 04 '16 at 18:43
  • @Trey As a side note, `int randomNumberBetween0and1 = rand.nextInt(1) + 0;` returns always 0. `rand.nextInt(1)` gives an int between zero (inclusive) and one (exclusive), so it is always 0. Also, why add the `+0` part? – J. Kamans Oct 04 '16 at 18:44
  • I just googled how to make a random number and that is what I was given. Do you mind pointing me in the right direction of how to make random numbers in a given range? – Trey Oct 04 '16 at 18:49
  • Random rand = new Random(); int value = rand.nextInt(50); This will give value from 0 to 49. For 1 to 50: rand.nextInt(50) + 1; Is this correct? – Trey Oct 04 '16 at 18:50
  • @Trey yep that's right, same as [here](http://stackoverflow.com/a/5887745/1248974) – chickity china chinese chicken Oct 04 '16 at 19:08

1 Answers1

2

numberOfTries is initialized outside of your loops. Once you try twice, it never gets set back to 0 which causes the loops to skip and finish on the next question because numberOfTries is already 2.

Brion
  • 746
  • 3
  • 10