在Java中的while循环顺序

huangapple go评论73阅读模式
英文:

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语句就可以解决问题 在Java中的while循环顺序

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 在Java中的while循环顺序

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

huangapple
  • 本文由 发表于 2020年9月16日 21:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定