在 while 循环中使用 switch 语句
Using switch statements in a while loop
我试图在 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");
}
如果我运行这段代码,输出结果如下:
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
当我输入金额时,我想选择另一个选项 - 输出应该根据所选的选项(您可能应该知道我想要这段代码做什么)。有人可以帮忙更正代码吗?
谢谢
您当前获取并设置选择值 一次 并且在 while 循环之前,因此无法从循环内更改它。解决方案:从 while 循环的 inside 中的 Scanner 对象获取下一个选择值。要理解这一点,请从逻辑上思考问题,并确保在头脑中和纸上仔细检查您的代码,因为该问题实际上不是编程问题,而是基本逻辑问题。
关于:
Could someone please help correct the code?
出于多种原因,请不要要求我们这样做。
- 这不是作业完成服务
- 当您通过编写代码来学习如何编码时,要求他人为您更改代码是在伤害自己。
- 确实,这是一个基本的简单问题,您有能力自行解决。请试一试,只有在尝试无效时,才向我们展示您的尝试。
case 必须大于 4,在你的情况下 cases 小于 4。所以你不会退出循环,基本上 break 语句会中断 switch 并跳转到循环,但循环又会变少比 4 所以它再次跳入开关,依此类推。修正你的案件的大小,也许只是做一个
(selection != 1 || selection != 2 || selection !=3 || selection !=4)
您忘记再次请求选择。一旦输入就不会改变。
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
}
您甚至可以使用 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);
我试图在 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");
}
如果我运行这段代码,输出结果如下:
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
当我输入金额时,我想选择另一个选项 - 输出应该根据所选的选项(您可能应该知道我想要这段代码做什么)。有人可以帮忙更正代码吗?
谢谢
您当前获取并设置选择值 一次 并且在 while 循环之前,因此无法从循环内更改它。解决方案:从 while 循环的 inside 中的 Scanner 对象获取下一个选择值。要理解这一点,请从逻辑上思考问题,并确保在头脑中和纸上仔细检查您的代码,因为该问题实际上不是编程问题,而是基本逻辑问题。
关于:
Could someone please help correct the code?
出于多种原因,请不要要求我们这样做。
- 这不是作业完成服务
- 当您通过编写代码来学习如何编码时,要求他人为您更改代码是在伤害自己。
- 确实,这是一个基本的简单问题,您有能力自行解决。请试一试,只有在尝试无效时,才向我们展示您的尝试。
case 必须大于 4,在你的情况下 cases 小于 4。所以你不会退出循环,基本上 break 语句会中断 switch 并跳转到循环,但循环又会变少比 4 所以它再次跳入开关,依此类推。修正你的案件的大小,也许只是做一个
(selection != 1 || selection != 2 || selection !=3 || selection !=4)
您忘记再次请求选择。一旦输入就不会改变。
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
}
您甚至可以使用 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);