简体   繁体   English

在“ while循环”中初始化或退出程序

[英]initialization in “while loop” or quit the program

so here i have a code and my problem is when i press 1 to quit the program i can type stop to quit the program but i want it to be like as soon as i press 1 i want to quit the program without typing stop. 所以这里我有一个代码,我的问题是当我按1退出程序时,我可以键入stop退出程序,但我希望它就像我按1一样就想退出程序而无需键入stop。 how should i fix it? 我该如何解决? because after press 1 and type something that is not "stop", it will start from the beginning 因为在按1并键入非“ stop”后,它将从头开始

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String text = "";

    while (!text.equals("stop")) {
        System.out.println("\n(1) To quit this program, Press 1");
        int starter = scan.nextInt();

        switch (starter) {
            case 1:
                System.out.println("Type Stop to quit this program`");
                System.out.println("Bye");
                text = br.readLine();
                break;
        }
    }

    scan.close();

}

代替使用break ,使用return将使方法退出。

In your case statement, when starter is equal to 1 , make text equal to stop .... 在您的case语句中,当starter等于1 ,使text等于stop ...。

switch (starter) {
    case 1:
        text = "stop";
        break;
}

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

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