简体   繁体   English

java While循环语句将继续循环,但不会包含我的退出循环的语句

[英]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. 我知道循环会停止,如果体内某些东西可以使条件为“ false”,但是对于该程序,当它询问是否要再次播放时,将决定该程序。 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. 因为您没有更改looping变量值。 And move below code outside else if() block. 然后将下面的代码移到else if()块之外。

...
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. 您可以在情况下使用while或do while循环。

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. 是一个非常有用的运算符,我们有时可以使用它在一行中压缩if-else块。
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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