简体   繁体   中英

java While-loop statement will continue to loop but won't include my statement to exit the loop

The program itself works, but I'm struggling with the loop. I'm a beginner using loops and I've been looking around for examples applicable to my type of program, but sadly haven't found any.

I know the loop stops if there is something within the body that can make the condition 'false' but for this program that would be decided when it asks the use if it would like to play again. However, my program is not asking the user if it wants to continue. And if the user decides to stop, then the program should say 'Thanks for playing!" Instead it continuously loops the whole body.

Thanks for the help!

This is my loop:

// Start loop
while (looping == true) {

    // Ask if the # > 5
    System.out.println("Is your number greater than 5? (True = 1, False = 0)");
    // Read in the number
    numberOne = input.nextDouble();

    if (numberOne == 1) { // Is the # > 7?
        System.out.println("Is your number greater than 7? (True = 1, False = 0)");
        numberOne = input.nextDouble();

        if (numberOne == 1) { // Does the # = 8?
            System.out.println("Is your number 8? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 8, then # = 9
                System.out.println("Your number is 9!");
            } else {
                // If answer was 8, then yay
                System.out.println("Yay! Got it!");
            }
        } else if (numberOne == 0) { // If # !> 7, then # = 6?
            System.out.println("Is your number 6? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 6, then # = 7
                System.out.println("Your number is 7!");
            } else {
                //If answer was 6, then yay
                System.out.println("Yay! Got it!");
            }
        }

    } else if (numberOne == 0) { // If the # !> 5, then # > 3?
        System.out.println("Is your number greater than 3? (True = 1, False = 0)");
        numberOne = input.nextDouble();

        if (numberOne == 1) { // If true, does your number = 4?
            System.out.println("Is your number 4? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 4, then # = 5
                System.out.println("Your number is 5!");
            } else {
                // If answer was 4, then yay
                System.out.println("Yay! Got it!");
            }
        } else if (numberOne == 0) { // If false, # = 2?
            System.out.println("Is your number 2? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 2, then # = 3
                System.out.println("Your number is 3!");
            } else {
                // If answer is 2, then yay
                System.out.println("Yay! Got it!");
            }

            // Ask user if they want to play again
            System.out.println("Would you like to play again? (Yes = 1, No = 0)");
            numberOne = input.nextDouble();

            System.out.println("Thanks for playing!");
        }
    }
} // end loop
// Close input
input.close();
} // end method
} // end class

Because you are not changing looping variable value. And move below code outside else if() block.

...
System.out.println("Would you like to play again? (Yes = 1, No = 0)");
looping = input.nextBoolean();

if (looping == false) {
    System.out.println("Thanks for playing!");
}
...

Your code will look like this

// Start loop
while (looping == true) {

    // Ask if the # > 5
    System.out.println("Is your number greater than 5? (True = 1, False = 0)");
    // Read in the number
    numberOne = input.nextDouble();

    if (numberOne == 1) { // Is the # > 7?
        System.out.println("Is your number greater than 7? (True = 1, False = 0)");
        numberOne = input.nextDouble();

        if (numberOne == 1) { // Does the # = 8?
            System.out.println("Is your number 8? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 8, then # = 9
                System.out.println("Your number is 9!");
            } else {
                // If answer was 8, then yay
                System.out.println("Yay! Got it!");
            }
        } else if (numberOne == 0) { // If # !> 7, then # = 6?
            System.out.println("Is your number 6? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 6, then # = 7
                System.out.println("Your number is 7!");
            } else {
                //If answer was 6, then yay
                System.out.println("Yay! Got it!");
            }
        }

    } else if (numberOne == 0) { // If the # !> 5, then # > 3?
        System.out.println("Is your number greater than 3? (True = 1, False = 0)");
        numberOne = input.nextDouble();

        if (numberOne == 1) { // If true, does your number = 4?
            System.out.println("Is your number 4? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 4, then # = 5
                System.out.println("Your number is 5!");
            } else {
                // If answer was 4, then yay
                System.out.println("Yay! Got it!");
            }
        } else if (numberOne == 0) { // If false, # = 2?
            System.out.println("Is your number 2? (True = 1, False = 0)");
            numberOne = input.nextDouble();

            if (numberOne == 0) { // If # != 2, then # = 3
                System.out.println("Your number is 3!");
            } else {
                // If answer is 2, then yay
                System.out.println("Yay! Got it!");
            }
        }
    }

    // Ask user if they want to play again
    System.out.println("Would you like to play again? (Yes = 1, No = 0)");
    looping = input.nextBoolean();

    if (looping == false) {
        System.out.println("Thanks for playing!");
    }
}
// Close input
input.close();
}
}
// for(intialise ; testing Condition; increment/decrement)
        for (int i = 0; i < 5; i++) {

        }

        // while(condition)
        // first the condition get evaluated, if true the loop body is executed
        int i = 0;
        while (i < 5) {
            // logic goes in here
            i++;
        }

        // do{
        // your code goes in here
        // }while(condition);
        // the loop gets executed once, then the condition is evaluated, if true
        // loop is executed again
        i = 0;
        do {

        } while (i < 5);

You can use a while or a do while loop in your case.

looping = true;
        while (looping) {
            // your code

            // Ask user if they want to play again
            System.out.println("Would you like to play again? (Yes = 1, No = 0)");
            numberOne = input.nextInt();
            //You can use a simple if else statement also
            if(numberOne == 1){
                looping = true;
            }
            else{
                lopping = false;
            }
        }

        // if you wish to use a do while loop:
        do {
            // your code

            numberOne = input.nextInt();
            // ternary operator
            looping = (numberOne == 1) ? true : false;

        } while (looping);

Ternary Opeartor:
Is a very useful operator and we can sometimes use it to condense the if-else block in a single line.
Take the case of finding max of two numbers.

 if(a > b){
            max = a;
       }else{
             max = b;
       }

Using a ternary operator this can be simplified and condensed into a single line:

max = (a>b)?a:b;

the general syntax is

(cond)?(value to return when cond true):(value to return if cond false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM