我写了这段代码,但它似乎不起作用。当我们输入 d
时,它会计算美元,但仍在执行(..什么?)。
你能看到 + 检测到列表部分中的 + 是错误的部分吗?
String currency = sc.next();
char detect = currency.charAt(0);
switch (detect){
case 'D':
case 'd':
double dollar = (amount/18*10);
System.out.println(amount + " Turkish Lira(s) --> " + dollar + " Dollar");
case 'E':
case 'e':
double euro = (amount/23*10);
System.out.println(amount + " --> " + euro + " Euro");
case 'T':
case 't':
double lira = (amount);
System.out.println(amount + " --> " + lira+ " Lira(s)");
while (detect!='d'|| detect!='e' || detect!='t' || detect!='D'|| detect!='E' || detect!='T'){
System.out.println("Can u See " + detect + " In The List ?\n" + menucur);
currency = sc.next();
detect = currency.charAt(0);
}
}
最佳答案
您需要在 switch 语句的每个 case 末尾添加 break
。
参见http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
关于java - Switch 语句意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329880/