Click here to Skip to main content
15,884,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have problem , I want when the user enter 1 ,Then add char x then if add char r
I want back to this list

Java
System.out.println("-------------------------------------");
                System.out.println("***             RIDES             ***");
                System.out.println("-------------------------------------");
                System.out.println("| X or x : UberX                    |");
                System.out.println("| L or l : UberXL                   |");
                System.out.println("| B or b : UberBlack                |");
                System.out.println("-------------------------------------");
                System.out.print(" > Please enter your choice...");
                UberX = t.next().charAt(0);


What I have tried:

Java
while (cho == 1) {

                System.out.println("-------------------------------------");
                System.out.println("***             RIDES             ***");
                System.out.println("-------------------------------------");
                System.out.println("| X or x : UberX                    |");
                System.out.println("| L or l : UberXL                   |");
                System.out.println("| B or b : UberBlack                |");
                System.out.println("-------------------------------------");
                System.out.print(" > Please enter your choice...");
                UberX = t.next().charAt(0);

                while (UberX == 'x') {
                    System.out.println("-------------------------------------");
                    System.out.println("***            OPTIONS            ***");
                    System.out.println("-------------------------------------");
                    System.out.println("| B or b:  Base fare                |");
                    System.out.println("| M or m:  Cost per minute          |");
                    System.out.println("| K or k:  Cost per km              |");
                    System.out.println("| S or s:  Service fee              |");
                    System.out.println("| C or c:  Cancellation fee         |");
                    System.out.println("| F or f:  Minimum fee              |");
                    System.out.println("| R or r:  Previous Menu            |");
                    System.out.println("-------------------------------------");
                    System.out.print(" > Please enter your choice...");
                    cho = t.next().charAt(0);

                    while (cho == 'b' || cho == 'B') {
                        System.out.println("Base fare for UberX is " + "3.2SR");
                        break;
                    }

                    while (cho == 'm' || cho == 'M') {
                        System.out.println("Cost per minute for UberX is " + "0.25SR");
                        break;
                    }

                    while (cho == 'k' || cho == 'K') {
                        System.out.println("Cost per km for UberX is " + "0.9SR");
                        break;
                    }

                    while (cho == 's' || cho == 'S') {
                        System.out.println("Service fee for UberX is " + "0SR");
                        break;
                    }
                    while (cho == 'c' || cho == 'C') {
                        System.out.println("Cancellation fee for UberX is " + "8SR");
                        break;
                    }

                    while (cho == 'f' || cho == 'F') {
                        System.out.println("Minimum fee for UberX is " + "8SR");
                        break;
                    }

                    while (cho == 'r' || cho == 'R') {
                        break;
                    }
                    
                    break;
                }
               break;
            }
Posted
Updated 17-Nov-17 13:37pm
Comments
CHill60 17-Nov-17 15:09pm    
Do you have a question?

The while loops comparing cho at the bottom should not be while loops. They should be ifs and breaking from the if would kick you out of the inner while loop (UberX == 'x') and back to the outer loop which is more like what you want.
 
Share this answer
 
Quote:
Problem in loops! !

I guess that loops are not your only problem.
Your systematic use of break inside each loop ensure that not a single loop will loop. You just defeat the purpose of loops.

Advice: go back to tutorials and learn properly how language structures works.
 
Share this answer
 

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