Java 在 while 循环中使用 switch 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32018562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:55:28  来源:igfitidea点击:

Using switch statements in a while loop

javawhile-loopswitch-statement

提问by Jspake

I'm trying to use switch statements in a while loop in Java, but there is something going wrong. Please have a look at a sample code below which explains my problem:

我正在尝试在 Java 的 while 循环中使用 switch 语句,但是出了点问题。请查看下面的示例代码,它解释了我的问题:

Scanner input=new Scanner(System.in);
    int selection = input.nextInt();

while (selection<4)
      {  switch(selection){
            case 1:
               System.out.println("Please enter amount");
               double amount=input.nextDouble(); //object of scanner class
               break;

            case 2:
               System.out.println("Enter ID number"); 
               break;

            case 3:
               System.out.println("Enter amount to be credited");
               break;
                          }
System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
     }

If I run this code, the output is as follows:

如果我运行这段代码,输出如下:

1
Please enter amount
2000
1. Transfer
2.Check balance
3.Recharge
Please enter amount
2
1. Transfer
2.Check balance
3.Recharge
Please enter amount

When I enter the amount, I would then like to choose another option - and the output should be according to the option chosen (you should probably be knowing what I want this code to do). Could someone please help correct the code?

当我输入金额时,我想选择另一个选项 - 输出应该根据选择的选项(您可能应该知道我想要这段代码做什么)。有人可以帮忙更正代码吗?

Thanks

谢谢

采纳答案by PC Luddite

You're forgetting to ask for the selection again. It's not going to change once it's been entered.

您忘记再次要求选择。一旦输入就不会改变。

Scanner input=new Scanner(System.in);
int selection = input.nextInt();

while (selection<4)
{
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
      selection = input.nextInt(); // add this
 }

You could even use a do...while loop instead to avoid writing input.nextInt();twice

你甚至可以使用 do...while 循环来避免写 input.nextInt();两次

Scanner input=new Scanner(System.in);
int selection;

do
{
   selection = input.nextInt();
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
 }
 while(selection < 4);

回答by Hovercraft Full Of Eels

You currently get and set the selection value onceand before the while loop, and so there is no way to change this from within the loop. The solution: Get your next selection value from the Scanner object insideof the while loop. To understand this, think the problem out logically and be sure to walk through your code mentally and on paper as the issue is not really a programming issue but rather a basic logic issue.

您当前在 while 循环之前获取并设置了一次选择值,因此无法从循环内更改此值。解决方案:从while 循环的 Scanner 对象获取下一个选择值。要理解这一点,请从逻辑上思考问题,并确保在精神上和纸上遍历您的代码,因为问题并不是真正的编程问题,而是一个基本的逻辑问题。



Regarding:

关于:

Could someone please help correct the code?

有人可以帮忙更正代码吗?

Please don't ask us to do this and for several reasons.

请不要要求我们这样做,原因有几个。

  1. This is not a homework completion service
  2. You're harming yourself by asking others to change the code for you, as you learn how to code by writing code.
  3. Really this is a basic simple issue that you have the ability to fix on your own. Please give it a try, and only if the attempt doesn't work, then show us your attempt.
  1. 这不是家庭作业完成服务
  2. 当您通过编写代码学习如何编码时,您要求他人为您更改代码是在伤害自己。
  3. 实际上,这是一个基本的简单问题,您可以自行解决。请尝试一下,只有在尝试不起作用时,才向我们展示您的尝试。

回答by Mike

Case must be bigger than 4, in your case the cases are less than 4. so you won't quit the loop, basically the break statement breaks the switch and jumps to loop, but than the loop is again less than 4 so it jumps again into the switch and so on. Fix the Sizes of your cases, maybe just make an

Case 必须大于 4,在你的情况下,cases 小于 4。所以你不会退出循环,基本上 break 语句会中断 switch 并跳转到循环,但循环再次小于 4 所以它跳转再次进入开关等。修复你的箱子的尺寸,也许只是做一个

(selection != 1 || selection != 2 || selection !=3 || selection !=4)