do-while问题! ! [英] Problem with do-while ! !
问题描述
我有问题,如何执行此代码?
-------------------------------------------------- -
###欢迎使用Uber票价计算器###
-------------------------------------------------- -
| 1:获取有关可用游乐设施的票价信息|
| 2:为您的旅途获取车费估算|
| 3:提交您的骑行评级|
| 4:退出并显示摘要|
-------------------------------------------------- -
>请输入您的选择...(1-4):5
选择无效!请重试
-------------------------------------------------- -
###欢迎使用Uber票价计算器###
-------------------------------------------------- -
| 1:获取有关可用游乐设施的票价信息|
| 2:为您的旅途获取车费估算|
| 3:提交您的骑行评级|
| 4:退出并显示摘要|
-------------------------------------------------- -
>请输入您的选择...(1-4):1
-------------------------------------
***骑*** ***
-------------------------------------
| X或x:UberX |
|升或升:UberXL |
| B或b:UberBlack |
| R或r:返回主菜单|
-------------------------------------
我尝试过的事情:
I have problem ,How can do this code?
----------------------------------------------------
### Welcome to Uber Fare Calculator ###
----------------------------------------------------
| 1 : Get fare information about available rides |
| 2 : Get a Fare Estimate for your Journey |
| 3 : Submit Rating of your Ride |
| 4 : Exit and display summary |
----------------------------------------------------
> Please enter your choice... (1-4):5
Invalid Choice!! Please Try again
----------------------------------------------------
### Welcome to Uber Fare Calculator ###
----------------------------------------------------
| 1 : Get fare information about available rides |
| 2 : Get a Fare Estimate for your Journey |
| 3 : Submit Rating of your Ride |
| 4 : Exit and display summary |
----------------------------------------------------
> Please enter your choice... (1-4):1
-------------------------------------
*** RIDES ***
-------------------------------------
| X or x : UberX |
| L or l : UberXL |
| B or b : UberBlack |
| R or r : Return to Main Menu |
-------------------------------------
What I have tried:
Scanner t = new Scanner(System.in);
int cho;
do {
System.out.println("----------------------------------------------------");
System.out.printf("%-11s%10s%11s", "###", "Welcome to Uber Fare Calculator", "###\n");
System.out.println("----------------------------------------------------");
System.out.println("| 1 : Get fare information about available rides |");
System.out.println("| 2 : Get a Fare Estimate for your Journey |");
System.out.println("| 3 : Submit Rating of your Ride |");
System.out.println("| 4 : Exit and display summary |");
System.out.println("----------------------------------------------------");
System.out.print(" > Please enter your choice... (1-4):");
cho = t.nextInt();
}while(cho<1 || cho>4);
我想当用户输入1,2,3或4时转到另一个循环,但是当用户输入另一个时,我想要此消息
I want when the user enter 1,2,3 or 4 go to another loops but when the user enter another choose i want this massage
System.out.println("\n\t\tInvalid Choice!! Please Try again");
推荐答案
do ... while
在条件为真时继续循环:
do ... while
continues to loop while teh condition is true:
i = 0;
do {
System.out.println(i);
i = i + 1;
} while (i < 5);
将打印:
0
1
2
3
4
仅当cho
小于1或大于4时,您的条件才成立:对于集{1、2、3、4}中的所有cho
值,它都是错误的.
因此,当用户输入有效的菜单选项时,它将立即退出循环.
更改条件,添加代码以处理菜单选项,它应该开始起作用.
Your condition is only true when cho
is either less than 1 or greater than 4: it is false for all values of cho
in the set {1, 2, 3, 4}
So when a user enters a valid menu choice, it immediately exits the loop.
Change your condition, add the code to handle your menu choices, and it should start to work.
这篇关于do-while问题! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!