gpt4 book ai didi

Java、Eclipse。如何让扫描仪同时读取小写和大写?

转载 作者:行者123 更新时间:2023-12-01 08:05:45 28 4
gpt4 key购买 nike

首先我想说你们很棒。我从你们所有人身上学到了很多东西。我的问题是如何让我的扫描仪读取用户的大写和小写输入?我的代码如下。

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”。

最佳答案

扫描仪支持正则表达式,因此您可以使用诸如 anotherScanner.hasNext("q|Q") 之类的内容

关于Java、Eclipse。如何让扫描仪同时读取小写和大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21770621/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com