Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何退出switch语句并返回While循环?_Java_Loops_While Loop_Switch Statement_Exit - Fatal编程技术网

Java 如何退出switch语句并返回While循环?

Java 如何退出switch语句并返回While循环?,java,loops,while-loop,switch-statement,exit,Java,Loops,While Loop,Switch Statement,Exit,所以我尝试的是,在我通过其中一个搜索得到足够的结果后,转到另一个搜索。换句话说,我想退出switch语句并返回while循环。我该怎么做 我将此作为代码: public static void main(String[] args) throws FileNotFoundException { String input = ""; Scanner scan = new Scanner(System.in); System.out.println("Hello, inp

所以我尝试的是,在我通过其中一个搜索得到足够的结果后,转到另一个搜索。换句话说,我想退出switch语句并返回while循环。我该怎么做

我将此作为代码:

public static void main(String[] args) throws FileNotFoundException {


    String input = "";
    Scanner scan = new Scanner(System.in);
    System.out.println("Hello, input witch method you shall use(name, amount or date): ");
    input = scan.next();

    Warehouse storage = new Warehouse();
    //todo while loop kad amzinai veiktu. Ar to reikia ?
    while (!input.equals("quit")) {
        switch (input) {
            case "name":
                storage.searchByName();
                break;
            case "amount":
                storage.searchByAmount();
                break;
            default:
                System.out.println("Wrong input!");

        }

    }
}

一旦你进入你的循环,你就永远不会更新
输入
,因此你会在
循环时(无限)返回到你的
。有几种方法可以做到这一点,一种是DoWhile循环。像

String helloMsg = "Hello, input which method you shall use(name, amount or date): ";
Scanner scan = new Scanner(System.in);
Warehouse storage = new Warehouse();
String input;
do {
    System.out.println(helloMsg);
    input = scan.next();
    switch (input) {
    case "name":
        storage.searchByName();
        break;
    case "amount":
        storage.searchByAmount();
        break;
    default:
        System.out.println("Wrong input!");
    }
} while (!input.equals("quit"));
另一个是无限循环,您可以使用
quit
来中断它。像

String helloMsg = "Hello, input which method you shall use(name, amount or date): ";
Scanner scan = new Scanner(System.in);
Warehouse storage = new Warehouse();
loop: while (true) {
    System.out.println(helloMsg);
    String input = scan.next();
    switch (input) {
    case "name":
        storage.searchByName();
        break;
    case "amount":
        storage.searchByAmount();
        break;
    case "quit":
        break loop;
    default:
        System.out.println("Wrong input!");
    }
}

注意:事实并非如此。

一旦你进入循环,你永远不会更新
输入
,因此你会在
循环(无限)时返回到
。有几种方法可以做到这一点,一种是DoWhile循环。像

String helloMsg = "Hello, input which method you shall use(name, amount or date): ";
Scanner scan = new Scanner(System.in);
Warehouse storage = new Warehouse();
String input;
do {
    System.out.println(helloMsg);
    input = scan.next();
    switch (input) {
    case "name":
        storage.searchByName();
        break;
    case "amount":
        storage.searchByAmount();
        break;
    default:
        System.out.println("Wrong input!");
    }
} while (!input.equals("quit"));
另一个是无限循环,您可以使用
quit
来中断它。像

String helloMsg = "Hello, input which method you shall use(name, amount or date): ";
Scanner scan = new Scanner(System.in);
Warehouse storage = new Warehouse();
loop: while (true) {
    System.out.println(helloMsg);
    String input = scan.next();
    switch (input) {
    case "name":
        storage.searchByName();
        break;
    case "amount":
        storage.searchByAmount();
        break;
    case "quit":
        break loop;
    default:
        System.out.println("Wrong input!");
    }
}

注意:它不是。

哦,很好,我没有想到在开关本身中使用“退出”!谢谢你的更正!哦,太好了,我没想到在开关本身中使用“退出”这个词!谢谢你的更正!