我找不到代码使用大写和小写字母的方法,我只能找到一种方法让它运行一个或另一个...尝试了一段时间但仍然没有管理,你看到的新鲜事!提前谢谢!
devise_for :users
resources :users do
resources :courses do
collection do
get 'my_courses'
post 'add_course'
end
end
end
root to: "courses#index"
答案 0 :(得分:6)
由于尚未发布交换机案例解决方案,因此它是:
switch (letter) {
case 'A':
case 'a':
System.out.println("Your class is at 10:00");
break;
case 'B':
case 'b':
System.out.println("Your class is at 12:00");
break;
case 'C':
case 'c':
System.out.println("Your class is at 14:00");
break;
default:
System.out.println("Your group does not exist, please select one that does...");
break;
}
就个人而言,我发现这是最具可读性的解决方案。
答案 1 :(得分:2)
为什么您不使用String
方法toUpperCase()
并仅检查大写变体?
String myClass = (letter + "").toUpperCase();
if (myClass.equals("A")) {
System.out.println("Your class is at 10:00");
} else {
if (myClass.equals("B")) {
System.out.println("Your class is at 12:00");
} else
if (myClass.equals("C")) {
System.out.println("Your class is at 14:00");
} else {
System.out.println("Your group does not exist, please select one that does...");
}
}
你也可以使用正则表达式:
答案 2 :(得分:1)
使用String.toLowerCase()
或String.toUpperCase()
方法。喜欢
if (letter.toLowerCase().equals("a")) {
System.out.println("Your class is at 10:00");
}
还有一件事,使用equals()
方法检查是否相等!
答案 3 :(得分:1)
您可以尝试更改while循环,如下所示。您当前的循环仅检查a
到d
。如果
- < A
- > d
- between D and a
do .. while((letter <'a' && letter > 'D') || letter < 'A' || letter > 'd');
答案 4 :(得分:1)
if(letter == 'A' || letter == 'a'){
System.out.println("Your class is at 10:00");
} else if(letter == 'B' || letter == 'b'){
System.out.println("Your class is at 12:00");
} else if (letter == 'C' || letter == 'c'){
System.out.println("Your class is at 14:00");
} else {
System.out.println("Your group does not exist, please select one that does...")
}
这是我查看代码更清晰的方法,不需要使用消耗更多资源的String函数。