While循环在不改变条件值的情况下终止。

huangapple 未分类评论44阅读模式
英文:

While loop terminates without changing the condition's value

问题

我正在使用这段代码:

```java
    public void menu() {
        boolean finish = false;
        while (!finish) {
            // 打印菜单
            int n = // 通过程序的其他方法获取选择
            switch (n) {
                case 0:
                    finish = true;
                    break;
                case 1:
                    // 执行操作 1
                    break;
                case 2:
                    // 执行操作 2
                    break;
                case 3:
                    // 执行操作 3
                    break;
                case 4:
                    // 执行操作 4
                    break;
            }
        }

    }

我尝试在所有的 break; 前面加上 finish = true;,但是 while 循环仍然会终止。
finish 变量在其他地方没有被使用。

我需要在 n == 0 之前使其不终止。


<details>
<summary>英文:</summary>

I&#39;m using this code:

public void menu() {
    boolean finish = false;
    while (!finish) {
        // print menu
        int n = // acquire selection through other method of the program
        switch (n) {
            case 0:
                finish = true;
                break;
            case 1:
                // do stuff 1
                break;
            case 2:
                // do stuff 2
                break;
            case 3:
                // do stuff 3
                break;
            case 4:
                // do stuff 4
                break;
        }
    }

}

I tried putting ```finish = true;``` right before all the ```break;``` but the while loop keeps terminating.
The ```finish``` variable is NOT used anywhere else.

I need this to not terminate until ```n == 0```

</details>


# 答案1
**得分**: 0

```java
我假设您只想在 n = 0 时终止输出。

public static void main(String[] args) {
    boolean finish = false;
    while (!finish) {
        // 打印菜单
        System.out.println("NEXT!");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        switch (n) {
            case 0:
                finish = true;
                break;
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            case 4:
                System.out.println("4");
                break;
        }
    }
}

上述代码有效
英文:

I assume you only want to terminal when n = 0

  public static void main(String[] args) {

    boolean finish = false;
    while (!finish) {
        // print menu
        System.out.println(&quot;NEXT!&quot;);
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        switch (n) {
            case 0:
                finish = true;
                break;
            case 1:
                System.out.println(&quot;1&quot;);
                break;
            case 2:
                System.out.println(&quot;2&quot;);
                break;
            case 3:
                System.out.println(&quot;3&quot;);
                break;
            case 4:
                System.out.println(&quot;4&quot;);
                break;
        }
    }
}

Code above works

答案2

得分: 0

public class Test {
    public static void main(String[] args) {
        WhileTest whileTest = new WhileTest();
        whileTest.menu();
        System.out.println("Just finished menu function");
    }
}

class WhileTest {
    public void menu() {
        boolean finish = false;
        while (!finish) {
            // 打印菜单
            int n = 0; // 通过程序的其他方法获取选择
            switch (n) {
                case 0:
                    finish = true;
                    System.out.println(0);
                    break;
                case 1:
                    // 执行操作 1
                    System.out.println(1);
                    break;
                case 2:
                    // 执行操作 2
                    System.out.println(2);
                    break;
                case 3:
                    // 执行操作 3
                    System.out.println(3);
                    break;
                case 4:
                    // 执行操作 4
                    System.out.println(4);
                    break;
            }
            System.out.println("After the switch block");
        }
    }
}

注意:在 switch 语句中没有给出默认条件。当在 switch 块中不提供默认语句时,代码仍然是有效的。然而,程序的执行将在 switch 块之后开始。

请注意以下几种情况:

情景1: 当 n = 0 时,输出如下:

0
After the switch block
Just finished menu function

程序首次进入 switch 块,并将 finish 变量设置为 true。因此,执行只会发生一次。

情景2: 当 n = 1、2、3 或 4 时,输出如下:

4
After the switch block
4
After the switch block
4
After the switch block

将会是上述两行输出的无限循环。finish 变量永远不会被设置为 true。您需要手动停止程序。

情景3: 当 n = 5 或任何不在 case 块中的值时,输出如下:

After the switch block
After the switch block
After the switch block
After the switch block
After the switch block
After the switch block

将会是上述行的无限循环。因为它不会进入任何 case 块,也没有地方修改变量 finish。

英文:
public class Test {
    public static void main(String[] args) {
        WhileTest whileTest = new WhileTest();
        whileTest.menu();
        System.out.println(&quot;Just finished menu function&quot;);
    }
}

class WhileTest {
    public void menu() {
        boolean finish = false;
        while (!finish) {
            // print menu
            int n = 0; // acquire selection through other method of the program
            switch (n) {
                case 0:
                    finish = true;
                    System.out.println(0);
                    break;
                case 1:
                    // do stuff 1
                    System.out.println(1);
                    break;
                case 2:
                    // do stuff 2
                    System.out.println(2);
                    break;
                case 3:
                    // do stuff 3
                    System.out.println(3);
                    break;
                case 4:
                    // do stuff 4
                    System.out.println(4);
                    break;
            }
            System.out.println(&quot;After the switch block&quot;);
        }

    }


}

Please note that you have not given any default condition in the switch statement. And when you do not give the default statement in the switch block, the code would be perfectly valid. However, the execution of the program begins after the switch block.

Note down the following scenario:

Scenario1: When you give n = 0. The output would be the following :

0
After the switch block
Just finished menu function

It goes the first time to the switch block and makes the finish variable to true. So execution will happen only once.

Scenario2: when you give n = 1 or 2 or 3 or 4. The output would be the following :

4
After the switch block
4
After the switch block
4
After the switch block

It will be infinite stream of the above 2 two lines. The finish variable is never made true. You will have to manually stop the program.

Scenario3: when you give n = 5 or anything which is not there in the case block. The output would be the following :

After the switch block
After the switch block
After the switch block
After the switch block
After the switch block
After the switch block

It will be an infinite stream of the above lines. Because it is not going to any of the case blocks nor the variable finish is getting modified anywhere.

huangapple
  • 本文由 发表于 2020年4月6日 23:54:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61063789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定