英文:
Order of while loops in Java
问题
我编写了一个类似这样的程序:
System.out.println("你想要做什么?");
String task = sc.nextLine();
然后根据输入,我有一系列针对每个输入的while循环:
while (task.equals("示例")) {
//做一些事情
System.out.println("你想要做什么?");
task = sc.nextLine();
}
while (task.equals("示例2")) {
//做一些事情
System.out.println("你想要做什么?");
task = sc.nextLine();
}
等等。在每个循环的结尾,用户可以输入他们想要做的下一步操作,根据他们的输入,相应的while循环应该执行。
但是我无法使位于另一个循环上方的while循环执行。所以,如果我在“示例”循环中,之后可以执行“示例2”循环,因为它在下面。但是,如果我在“示例2”循环中,无法执行位于其上方的“示例”循环。有没有解决这个问题的方法呢?
英文:
I wrote a program that looks something like this
System.out.println ("What would you like to do?");
String task = sc.nextLine();
Then based on the input, I have a bunch of while loops for each input
while (task.equals("example")) {
//do something
System.out.println ("What would you like to do now?");
task = sc.nextLine();
}
while (task.equals("example 2")) {
//do something
System.out.println ("What would you like to do now?");
task = sc.nextLine();
}
etc. At the end of each loop the user can input what they want to do next, and depending on what they say, the corresponding while loop should execute.
But I can't get a while loop that's above another one to execute. So if I'm in the "example" loop, I can execute the "example 2" loop afterwards because it is below it. But if I'm in the "example 2" loop, I can't execute the "example" loop which is above it. Is there any way around this?
答案1
得分: 2
是的,你可以这样处理,将所有内容都放在一个循环中,然后一个简单的if语句就可以解决问题
String task;
while(true) {
System.out.println("您想要做什么?");
task = sc.nextLine();
if(task.equals("示例")) {
//进行某些操作
} else if(task.equals("示例2")) {
//进行某些操作
}
// 其他判断...
else {
break; // 您还可以使用此语句来跳出循环
}
}
英文:
Yes, the way you could go around it is to pack it all up in a single loop and then a simple if statement should do the trick
String task;
while(true) { //or !task.equals("exit")
System.out.println("What would you like to do now?");
task = sc.nextLine();
if(task.equals("example") {
//do something
}else if(task.equals("example 2") {
//do something
}
...
}else { break; } // You can also use this to break out of your loop
}
答案2
得分: 1
public static void main(String[] args) {
String task;
Scanner sc = new Scanner(System.in);
do {
System.out.println("What would you like to do?");
task = sc.nextLine();
switch (task) {
case "example":
System.out.println("example 1");
break;
case "example 2":
System.out.println("example 2");
break;
default:
break;
}
} while (!"exit".equals(task));
System.out.println("task exit");
}
英文:
oooh, a loop means loop, you can do a lot of thing in it. so you can do it with just one loop:
public static void main(String[] args) {
String task;
Scanner sc = new Scanner(System.in);
do {
System.out.println("What would you like to do?");
task = sc.nextLine();
switch (task) {
case "example":
System.out.println("example 1");
break;
case "example 2":
System.out.println("example 2");
break;
default:
break;
}
} while (!"exit".equals(task));
System.out.println("task exit");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论