在 while 循环中使用 switch 语句 [英] Using switch statements in a while loop

查看:133
本文介绍了在 while 循环中使用 switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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
2.Check balance
3.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?

谢谢

推荐答案

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

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
2.Check balance
3.Recharge");
      selection = input.nextInt(); // add this
 }

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

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

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
2.Check balance
3.Recharge");
 }
 while(selection < 4);

这篇关于在 while 循环中使用 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆