繁体   English   中英

While 循环的替代方案?

[英]Alternative for a While loop?

   private static void execute(Cheese[] cheeses) {
        int choice;  
        System.out.println("\n\nNow displaying the cheeses");
        choice = getChoice();
        while (true) {
            if (choice == 1)
                sortByName(cheeses);
            else if (choice == 2)
                sortByCost(cheeses);
            else if (choice == 3)
                sortByAge(cheeses);
            else if (choice == 4)
                System.exit(0);
            else
                System.out.print("Invalid entry - please reenter.");
            choice = getChoice();
        }
    }

基本上,我正在寻找这个循环的替代方案。 我想用一个输入字符串做一个 do 循环,但我很难打印它。 有没有办法让 do while 循环不做任何事情或要求输入?

您在每种情况下都缺少break 为了演示它,我使用循环调用了execute()五次。 为了模拟不同选项的调用,我随机生成了choice

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Call#" + i);
            execute();
            System.out.println();
        }
    }

    private static void execute() {
        int choice;
        choice = getChoice();
        while (true) {
            System.out.println("Choice: " + choice);
            if (choice == 1) {
                System.out.println("sortByName");
                break;
            } else if (choice == 2) {
                System.out.println("sortByCost");
                break;
            } else if (choice == 3) {
                System.out.println("sortByAge");
                break;
            } else if (choice == 4) {
                System.out.println("Goodbye!");
                break;
            } else {
                System.out.println("Invalid entry - please reenter.");
                choice = getChoice();
            }
        }
    }

    static int getChoice() {
        return new Random().nextInt(10);
    }
}

示例运行:

Call#1
Choice: 6
Invalid entry - please reenter.
Choice: 8
Invalid entry - please reenter.
Choice: 1
sortByName

Call#2
Choice: 4
Goodbye!

Call#3
Choice: 0
Invalid entry - please reenter.
Choice: 5
Invalid entry - please reenter.
Choice: 3
sortByAge

Call#4
Choice: 0
Invalid entry - please reenter.
Choice: 8
Invalid entry - please reenter.
Choice: 0
Invalid entry - please reenter.
Choice: 7
Invalid entry - please reenter.
Choice: 2
sortByCost

Call#5
Choice: 8
Invalid entry - please reenter.
Choice: 3
sortByAge

但是,我会使用switch...case如下来保持代码干净:

private static void execute() {
    int choice;
    choice = getChoice();
    while (true) {
        System.out.println("Choice: " + choice);
        switch (choice) {
        case 1:
            System.out.println("sortByName");
            return;
        case 2:
            System.out.println("sortByCost");
            return;
        case 3:
            System.out.println("sortByAge");
            return;
        case 4:
            System.out.println("Goodbye!");
            return;
        default:
            System.out.println("Invalid entry - please reenter.");
            choice = getChoice();
        }
    }
}

我希望它有帮助。 如有任何疑问/问题,请随时发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM