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

时间:2015-10-07 05:17:37

标签: java loops while-loop

程序本身有效,但我在循环中挣扎。我是一个使用循环的初学者,我一直在寻找适合我的程序类型的例子,但遗憾的是找不到任何例子。

我知道如果身体内有某些东西可以使条件“假”,那么循环就会停止。但对于这个程序,当它要求使用时,如果它想再次播放,将决定。但是,我的程序不会询问用户是否要继续。如果用户决定停止,那么该程序应该说“感谢您的发挥!”#34;相反,它不断循环整个身体。

感谢您的帮助!

这是我的循环:

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

2 个答案:

答案 0 :(得分:2)

因为您没有更改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();
}
}

答案 1 :(得分:0)

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

Ternary Opeartor:
是一个非常有用的运算符,我们有时可以使用它来压缩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);