繁体   English   中英

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

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

该程序本身可以工作,但是我在循环中挣扎。 我是一个使用循环的初学者,我一直在寻找适用于我的程序类型的示例,但可惜没有找到任何示例。

我知道循环会停止,如果体内某些东西可以使条件为“ false”,但是对于该程序,当它询问是否要再次播放时,将决定该程序。 但是,我的程序没有询问用户是否要继续。 而且,如果用户决定停止播放,则程序应说“谢谢您的游戏!”,而是不断循环播放整个身体。

谢谢您的帮助!

这是我的循环:

// 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

因为您没有更改looping变量值。 然后将下面的代码移到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!");
}
...

您的代码将如下所示

// 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);

您可以在情况下使用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);

三元操作者:
是一个非常有用的运算符,我们有时可以使用它在一行中压缩if-else块。
以找到两个数字最大的情况为例。

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

使用三元运算符,可以简化并压缩为一行:

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

通用语法是

(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