循环中的问题! ! [英] Problem in loops! !

查看:85
本文介绍了循环中的问题! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有问题,我想当用户输入1,然后添加char x然后添加char r

我想回到这个列表



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

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:

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;
            }

推荐答案

比较cho底部的while循环不应该是while循环。它们应该是ifs并且从if开始将你踢出内部while循环(UberX =='x')并返回外部循环,这更像你想要的。
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.


Quote:

循环问题! !



我猜这个循环不是你唯一的问题。

你系统使用打破里面每个循环确保不会有一个循环循环。你只是打败了循环的目的。



建议:回到教程并正确学习语言结构如何运作。


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.


这篇关于循环中的问题! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆