虽然循环不起作用

时间:2014-03-27 01:37:55

标签: java loops while-loop

我对java比较陌生。我的内在while循环不起作用?可能是什么原因?

我试图先在while循环中运行while循环,然后继续前进到最外面的while循环。这不是你如何操作这样的场景吗?

Scanner scan = new Scanner(System.in);

    int count = 0;
    while (count>=0)
    {
        int num = 0;
        while(num<1 && num>4)
        {
            System.out.println("Which aircraft would you like to simulate?");
            System.out.println("1. Blimp");
            System.out.println("2. Helicopter");
            System.out.println("3. Fighter Jet");
            System.out.println("4. Space Shuttle");
            num = scan.nextInt();
            int num2 = 0;
            while(num2 < 1 && num2>6) 
            {
                System.out.println("Please select your programmable characterisitics for simulated flight.");
                System.out.println("1. Position Trim ");
                System.out.println("2. Force Breakout");
                System.out.println("3. Force Gradient");
                System.out.println("4. Force Friction");
                System.out.println("5. Damping");
                System.out.println("6. Hard Stop");
                num2 = scan.nextInt();
                if(num2 == 1)
                {
                    System.out.println("The position to which a flight control returns");
                }
                else if(num2 == 2)
                {
                    System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
                }
                else if(num2 == 3)
                {
                    System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
                }
                else if (num2 == 4)
                {
                    System.out.println("A constant force that is opposite to the direction of movement");
                }
                else if(num2 == 5)
                {
                    System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
                }
                else if (num2 == 6)
                {
                    System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
                }
                else
                {
                    System.out.println("Invalid input");
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

while(num < 1 && num > 4)将始终评估为false

while(num2 < 1 && num2 > 6)也将始终评估为false

如果您更改了<>标志,那么您的while循环应该可以更好地运行:)

答案 1 :(得分:1)

程序从上到下流动,而不是从内到外。确实在外部之前检查内部循环,但这是因为内部循环的底部总是在外部底部之前到达。无论如何。 。

你需要||,而不是&amp;&amp ;.如果输入小于1或大于4(或6),则希望循环继续。做... while()可能会更好。

其次,你的外环似乎什么都不做。

这是一个有效的修复方法:

public class MyClass {

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

        int num = 0;
        while (num < 1 || num > 4) {
            System.out.println("Which aircraft would you like to simulate?");
            System.out.println("1. Blimp");
            System.out.println("2. Helicopter");
            System.out.println("3. Fighter Jet");
            System.out.println("4. Space Shuttle");
            num = scan.nextInt();
            int num2 = 0;
            while (num2 < 1 || num2 > 6) {
                System.out.println("Please select your programmable characterisitics for simulated flight.");
                System.out.println("1. Position Trim ");
                System.out.println("2. Force Breakout");
                System.out.println("3. Force Gradient");
                System.out.println("4. Force Friction");
                System.out.println("5. Damping");
                System.out.println("6. Hard Stop");
                num2 = scan.nextInt();
                if (num2 == 1) {
                    System.out.println("The position to which a flight control returns");
                } else if (num2 == 2) {
                    System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
                } else if (num2 == 3) {
                    System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
                } else if (num2 == 4) {
                    System.out.println("A constant force that is opposite to the direction of movement");
                } else if (num2 == 5) {
                    System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
                } else if (num2 == 6) {
                    System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
                } else {
                    System.out.println("Invalid input");
                }
            }
        }
    }
}

答案 2 :(得分:1)

查看代码,看起来你想要运行循环的内容,然后测试条件。

在这种情况下,您希望使用do ... while循环。

另外,正如ProgrammerDan所指出的那样,你要测试的条件是不可能的。 num不能小于1 大于4,因此循环不会继续超过第一次运行。如果要在num超出1-4范围时重新运行循环,则测试该num小于1 ||)大于4。

int num = 0;
do
{
    //
    // your loop contents here
    //
} while(num<1 || num>4);