首先,我想说你们很棒。我从大家那里学到了很多东西。我的问题是如何让我的扫描仪读取用户的大小写输入?我的代码如下。
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c;
while (!usersSelection) {
System.out.println("Selection: ");
if (anotherScanner.hasNext("q")) {
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected to quit");
}
if (anotherScanner.hasNext("t")) {
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected the coin toss estimator");
}
if (anotherScanner.hasNext("g")) {
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected the grade estimator");
}
if (anotherScanner.hasNext("c")) {
c = anotherScanner.next();
usersSelection = true;
System.out.print("You have selected color Challenge");
} else {
String zoom = anotherScanner.next();
System.out.println("You have entered an invalid option. '"
+ zoom + "' is not a valid option.");
}
}
我想根据用户的选择(不区分大小写)进行操作,即用户是否输入“Q”或“q”。
答案 0 :(得分:4)
扫描仪支持正则表达式,因此您可以使用anotherScanner.hasNext("q|Q")
代替
答案 1 :(得分:2)
改为此,
1)。
anotherScanner.hasNext("q") || anotherScanner.hasNext("Q")
2)。
anotherScanner.hasNExt("q|Q");
实施任何一个。