当用户输入 0 时如何结束 do-while 循环?
如果用户输入 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
}
}