Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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);


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
Java
System.out.println("\n\t\tInvalid Choice!! Please Try again");
Posted
Updated 14-Nov-17 8:06am

1 solution

do ... while continues to loop while teh condition is true:
Java
i = 0;
do {
   System.out.println(i);
   i = i + 1;
   } while (i < 5);
Will print:
0
1
2
3
4
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.
 
Share this answer
 
Comments
Member 13511450 14-Nov-17 14:10pm    
Where I can add this code
System.out.println("\n\t\tInvalid Choice!! Please Try again");
OriginalGriff 14-Nov-17 14:25pm    
As part of the code you have yet to write to handle your menu choices! If you don't find a valid choice, you print that and go round again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900