gpt4 book ai didi

java - 我需要为扫描仪输入请求 'Case in-sensitive' 代码。接受任何建议

转载 作者:行者123 更新时间:2023-11-29 08:24:41 27 4
gpt4 key购买 nike

我想知道:有没有办法让读取扫描仪输入 '不区分大小写' whitout 小写或大写?我必须做出很多“Y”或“N”的回答,我必须将每个扫描仪输入转换为字符串,然后使用“.toUpperCase”“.toLowerCase”。 我觉得我缺少一些更实用的东西......

我的另一个想法是创建一个扫描器类,将每个输入转换为字符串,然后转换为大写/小写,但我的问题基本上是,是否有任何方法可以从一开始就设置它,我可能会丢失...

public void acariciar(ArrayList<Animal> animales) {
System.out.println("Quiere acariciar a estos animales?? Y ó N");

String inputDeUsuario = escaner.nextLine();
if (inputDeUsuario.toUpperCase().equals("Y")) {
for (Animal animal : animales) {
animal.acariciado();
}
}
}

最佳答案

不存在“不区分大小写的输入”之类的东西——拉丁字母本质上要么是大写要么是小写。但是您可以要求比较忽略被比较字符串的每个字符对之间的大小写差异,使用String.equalsIgnoreCase()。 :

inputDeUsuario.equalsIgnoreCase("Y")

(如果您处于无法控制相等比较的情况下,例如,如果您要将字符串添加到 HashSet,那么您的做法是正确的:选择小写或大写,并将所有内容转换为该大小写。)

关于java - 我需要为扫描仪输入请求 'Case in-sensitive' 代码。接受任何建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076337/

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