如何在用户输入0时结束do-while循环?结束这个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
}
}
如果你能避免它,破坏是一个坏习惯。 – bitmask 2011-06-08 09:59:44
感谢它的工作! :DD – user788949 2011-06-08 10:55:03
@ user788949:当输入不是“O”时,你的循环总是会中断,那么可疑的while-condition'ch =='O''怎么办?我的意思是,如果它仍然被你的规格所破坏,它怎么能起作用? – 2011-06-08 12:05:11