java - Switch 语句意外结果

标签 java switch-statement

我写了这段代码,但它似乎不起作用。当我们输入 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/

相关文章:

c++ - (C++/QT) 通过将字符串转换为 int 来使用带有字符串的 switch 语句

ruby - 为什么 Array 不覆盖 Ruby 中的三重等号方法?

c - switch-case 中有效但毫无值(value)的语法?

java - 如何从 ble 设备获取正确(解码)的制造商广告数据?

java - 从标签匹配二进制文件的内容 (Java)

java - 如何创建带标签的分隔符?

c - 开关盒基本型

java - 实体没有表单对象所需的字段

java - 如何将 Image View 和 TexView 左右对齐?

ios - 枚举类型的开关大小写