简体   繁体   English

结束这个 do-while 循环?

[英]Ending this do-while loop?

How do I end the do-while loop when the user enters 0?当用户输入 0 时如何结束 do-while 循环?

The program will continue execution if the user enter F,G,H and J. The program will exit if the user enters 0.如果用户输入 F、G、H 和 J,程序将继续执行。如果用户输入 0,程序将退出。

import java.util.Scanner;

public class P4Q5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        System.out.println("\nMain Menu: \n" +
                "Enter 0 to exit program\n" +
                "Enter F to display Faith\n" +
                "Enter G to display Grace\n" +
                "Enter H to display Hope\n" +
                "Enter J to display Joy\n");



        do {
             System.out.print("Enter your choice:");
               String s = sc.nextLine();
            char ch = s.charAt(0);
            if (( ch == 'F'))  {
                System.out.println("\nFaith\n");
            }

            else if  (( ch == 'G')) {
                System.out.println("\nGrace\n");
            }

            else if  (( ch == 'H')) {
                System.out.println("\nHope\n");
            }

            else if  (( ch == 'J')) {
                System.out.println("\nJoy\n");
            }



            else  {
                System.out.println("\nWrong option entered!!\n");
            }

        } while (ch == 'O');

              // TODO code application logic here
    }

}

How about while (ch != '0') instead of while (ch == 'O') ? while (ch != '0')而不是while (ch == 'O')怎么样? Notice the difference between 0 and O ?注意0O之间的区别?

try this in your do while:在你做的时候试试这个:

if( ch == '0') break;

Try this:尝试这个:

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("\nMain Menu: \n" +
        "Enter 0 to exit program\n" +
        "Enter F to display Faith\n" +
        "Enter G to display Grace\n" +
        "Enter H to display Hope\n" +
        "Enter J to display Joy\n");



do {
     System.out.print("Enter your choice:");
       String s = sc.nextLine();
    char ch = s.charAt(0);
    if (( ch == 'F'))  {
        System.out.println("\nFaith\n");
    }

    else if  (( ch == 'G')) {
        System.out.println("\nGrace\n");
    }

    else if  (( ch == 'H')) {
        System.out.println("\nHope\n");
    }

    else if  (( ch == 'J')) {
        System.out.println("\nJoy\n");
    }

    else if (( ch == 'O' )) {
        System.exit();
    }

    else  {
        System.out.println("\nWrong option entered!!\n");
    }

} while (ch == 'F' || ch == 'G' || ch == 'H' || ch == 'J' || ch == 'O');

      // TODO code application logic here

} }

To exit program you need to do System.exit()要退出程序,您需要执行 System.exit()

To exit loop do as @bitmask stated要退出循环,请按照@bitmask 所述操作

I would use a boolean variable, if you enter 0 the boolean becomes true, and then do a check on the boolean...我会使用 boolean 变量,如果输入 0,则 boolean 变为真,然后检查 boolean...

    boolean bool = false;
    do {
         ...

        if(input == '0')
           bool=true;
    } while (!bool);

oh and before i forget, i would also do one input before the do while, and one at the end of the loop.哦,在我忘记之前,我还会在 do while 之前输入一个输入,在循环结束时输入一个。 Like that, your whole code won't be run again after you hit 0.像这样,您的整个代码在您点击 0 后将不会再次运行。

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

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