如何在用戶輸入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