我非常喜欢Java中的初学者。我正在做一个简单的开关,用户在其中输入一个数字。为了训练,我添加了一个“for”循环。“for”循环和toLowerCase()方法中的简单开关
package JavaExc;
import java.util.Scanner;
public class JavaStart {
public static void main(String[] args) {
Scanner sck = new Scanner(System.in);
System.out.println("Type loop counter ");
int counter = sck.nextInt();
System.out.println("Counter is: " + counter);
for (int i = 0; i <= counter; i++) {
System.out.println("Type number in words");
String ch = sck.nextLine();
switch (ch.toLowerCase()) {
case "one":
System.out.println("CHOICE 1");
break;
case "Two":
System.out.println("CHOICE 2");
break;
case "THREE":
System.out.println("CHOICE 3");
break;
case "FoUr":
System.out.println("CHOICE 4");
break;
case "fiVE":
System.out.println("CHOICE 5");
break;
default:
System.out.println("Wrong Data");
break;
}
System.out.println("Loops left:\n " + counter--);
}
System.out.println("End of Switch");
}}
下面是结果:
Type loop counter
5
Counter is: 5
Type number in words
Wrong Data // ??
Loops left:
5
Type number in words
one
CHOICE 1
Loops left:
4
Type number in words
three // Should be ok, so what is wrong here?
Wrong Data
Loops left:
3
End of Switch //To early break loop I think
Process finished with exit code 0
我的问题是:为什么第一循环设为默认代码?我应该制作2台扫描仪吗?一个用于计数器,另一个用于开关? 为什么计数器和单词中的数字无法正常工作?
我知道我可以用表等来做,但一般来说,这个程序的目的是测试“toLowerCase()”方法。
ch的价值是多少? –
如果您将ch转换为小写,您将永远不会看到大写字母的情况。“THREE”,“FoUr”和“fiVE”的要点是什么? – CubeJockey
“三”和“三”不一样。 Java是区分大小写的 – Stultuske