所以我有一个非常烦人的问题,我希望你们中的一个人可以帮助我解决。这是一个非常简单的程序,用星号打印我的 COMP science 用户名。我为用户附加了一个选择 - 打印星号中的用户名或仅打印字符。
我构建了一个 while 循环来验证用户输入的数据是否准确。这个 while 循环的条件永远不会满足 - 因此无论输入什么,它总是循环遍历它。我确信这是一个非常简单的问题,只是之前没有真正使用过字符,所以无法弄清楚我做错了什么。
//===================================================== START OF MAIN
public static void main (String [] args){
Scanner input = new Scanner(System.in); // Scanner for users input
char usersChoice = 0; // Variable for users input
System.out.println("\nWould you like to see the large letters or the small letters?\n (Enter L for large, S for small!!)\n");
usersChoice = input.next().charAt(0); // Users Input
System.out.println(usersChoice);
//================================================= WHILE LOOP TO CHECK AUTHENTICITY OF DATA
while (usersChoice != 'l' || usersChoice != 'L' || usersChoice != 's' || usersChoice != 'S'){
System.out.println("\nWrong Input - Please try again!!!\n");
usersChoice = input.next().charAt(0);
System.out.println(usersChoice);
}//end of while
//================================================= IF (CHAR = L) PRINT BIG LETTERS
if (usersChoice == 'L' || usersChoice == 'l'){
printU();
print4();
printJ();
printA();
}//end of if
//================================================= ELSE PRINT LETTERS
else{
System.out.println("\nU");
System.out.println("4\n");
System.out.println("J\n");
System.out.println("A\n");
}//end of else
}//end of main
最佳答案
while
语句表达式始终为 true
,因为并非所有表达式都能同时为 true - 您需要条件 &&
运算符
while (usersChoice != 'l' && usersChoice != 'L' && usersChoice != 's' && usersChoice != 'S') {
关于java - 在 while 循环中比较 char --- 条件从未满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26411274/