【问题标题】:Java: Switch inside do while loopJava:在do while循环内切换
【发布时间】:2021-10-22 13:34:09
【问题描述】:

我尝试制作多个随机选择,以打印每个输入数字的消息,即一系列数字 (4 2 17 0),其中 0 将停止代码。输出错误


public class Main {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int number=scanner.nextInt();
       
  int x = number;
do{switch(x) {

case 1:
      System.out.println("Language selection");
break;
case 2:
       System.out.println("Customer support");
break;
case 3:
       System.out.println("Check the balance");
break;

case 4:
       System.out.println("Check loan balance");
break;
case 0:
System.out.println("Exit");
break;
  
default:
return;

}
if (x==0)
{break;}
x++;
}

while (true);

   }
} ```
![enter image description here](https://i.stack.imgur.com/RJBYn.jpg)

![enter image description here](https://i.stack.imgur.com/wCm4p.jpg)

【问题讨论】:

  • 在每个 case 语句中都需要一个break,否则它们就会失败。然后你需要更好的逻辑来退出循环(比如一个标签或设置一个布尔值并检查它而不是使用while(false)
  • 谢谢,现在尝试使用 break,没有帮助。我必须使用 do while 作为锻炼的一部分
  • 另外,while(false) 应该真的是while(true),否则你将在第一次迭代后立即退出循环,而不管其余代码做什么。 (while 条件为真而不是假时循环)
  • 使用 while(true) 得到每个测试的“执行超时”
  • 我刚刚注意到do...while 循环基本上什么都不做。你不应该有switch inside 循环吗?无论如何,如果你在这方面遇到困难,你可能应该重新阅读你用来学习 Java 的任何材料。因为我知道 下一个问题将是关于循环继续再次 因为您需要循环的退出条件。相反,请重新研究循环、条件和switch 的工作原理,了解您的程序在做什么并尝试重新编写它。

标签: java switch-statement do-while break


【解决方案1】:

您可以使用Scanner#hasNextInt 在整数用完时终止循环。

演示:

import java.util.Scanner;

public class Main {

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

        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            case 0:
                System.exit(0);
            }
        }
    }
}

示例运行:

4 2 17 0
Check loan balance
Customer support

另外,请注意,您需要 break 来避免案例不经意间落空。在这种情况下,我没有看到 default 的任何用法,因此,我已将其从演示代码中删除。

或者,使用 do-while 循环:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;

        do {
            x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            }
        } while (x != 0);
    }
}

【讨论】:

  • 太棒了!谢谢!有用! ?```导入java.util.Scanner;公共类 Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()){ int x=scanner.nextInt(); switch(x) { case 1: System.out.println("语言选择");休息;案例2:System.out.println("客户支持");休息;案例3:System.out.println("查看余额");休息;案例4:System.out.println("查询贷款余额");休息;案例 0:System.out.println("Exit"); System.exit(0); }}}}```
猜你喜欢
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多