Click here to Skip to main content
13,248,927 members (40,144 online)
Rate this:
 
Please Sign up or sign in to vote.
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:

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
System.out.println("\n\t\tInvalid Choice!! Please Try again");
Posted 4 days ago
Updated 4 days ago

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

do ... while continues to loop while teh condition is true:
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.
  Permalink  
Comments
Member 13511450 4 days ago
   
Where I can add this code
System.out.println("\n\t\tInvalid Choice!! Please Try again");
OriginalGriff 4 days ago
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 14 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100