Java,Eclipse。如何允许扫描仪同时读取大写和小写?
首先我想说你们真是太棒了。我从你们身上学到了很多。我的问题是如何允许扫描仪读取用户输入的大写和小写字符??我的代码如下Java,Eclipse。如何允许扫描仪同时读取大写和小写?,java,eclipse,java.util.scanner,uppercase,lowercase,Java,Eclipse,Java.util.scanner,Uppercase,Lowercase,首先我想说你们真是太棒了。我从你们身上学到了很多。我的问题是如何允许扫描仪读取用户输入的大写和小写字符??我的代码如下 Scanner anotherScanner = new Scanner(System.in); boolean usersSelection = false; String c; while (!usersSelection) { System.out.println("Selection: "); if (anotherScanner.hasNext("q"
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”。扫描仪支持正则表达式,因此您可以使用另一个扫描仪。hasNext(“Q | Q”)扫描仪支持正则表达式,因此您可以使用另一个扫描仪。hasNext(“Q”)扫描仪支持正则表达式(“q | q”)改为 1) 2) 实现任何人。更改为此 1) 2)
实现任何人。你是说你的扫描器不区分大小写吗?它应该自动做到这一点。我想他们是说他们希望
hasNext(“x”)
不区分大小写。scanner
支持正则表达式,所以类似于anotherScanner.hasNext(“q | q”)
mayborkmaybork?maybork?@MadProgrammer,你的意思是“will-work”。当然,假设OP真的在问这个问题。这是值得商榷的部分。当我运行程序时,它会提示我选择(g,c,t,q)。例如,如果我输入c,它将显示为“you have selected color challenge.”但如果我输入c(大写)它显示“您输入了一个无效的选项。我的问题是,如何让扫描仪忽略大小写,或者将用户输入的内容转换为大写形式来阅读?您是说扫描仪不区分大小写吗?它应该自动做到这一点。我想他们是说他们想要hasNext(“x”)
不区分大小写。Scanner
支持正则表达式,因此类似于另一个Scanner.hasNext(“q | q”)可能工作?可能工作?@MadProgrammer,您的意思是“将工作”当然,假设OP真的在问这个问题。这是值得讨论的部分。当我运行程序时,它会提示我选择(g,c,t,q),例如,如果我输入c,它会显示“你选择了颜色挑战”。但如果我输入c(大写),它会显示“您输入的选项无效。我的问题是,如何使扫描仪忽略大小写,或将用户输入的内容转换为大写形式来读取?
anotherScanner.hasNext("q") || anotherScanner.hasNext("Q")
anotherScanner.hasNExt("q|Q");