Switch 语句意外结果

问题描述 投票:0回答:4

我写了这段代码,但它似乎不起作用。当我们输入

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);
            } 
    }
java switch-statement
4个回答
4
投票

您需要在 switch 语句的每个 case 末尾添加

break

参见 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html


2
投票

在每个

break
块中每一系列语句的末尾添加
case
,例如:

switch (detect){
    case 'D':
    case 'd':
        double dollar = (amount/18*10);
        System.out.println(amount + " Turkish Lira(s) --> " + dollar + " Dollar");
        break; // <==== Add this

    case 'E':
    case 'e':
        double euro = (amount/23*10);
        System.out.println(amount + " --> " + euro + " Euro");
        break; // <==== Again here

...等等。这告诉代码您不想继续执行下一个

case

我建议阅读一本关于 Java 的优秀入门读物。在评论中,MadProgrammer 指出了一个专门针对

switch
的教程,here,但我会退后一步,对基础知识进行一些回顾。

为此,这里有一个正确编写的

switch
语句的示例:

// Assume `n` is an integer
switch (n) {
    case 0:
    case 1:
        System.out.println("n is 0 or 1");
        break;

    case 2:
    case 3:
    case 4:
        System.out.println("n is 2, 3, or 4");
        break;

    case 17:
        System.out.println("n is 17");
        break;

    default:
        System.out.println("n has some value other than 0, 1, 2, 3, 4, or 17");
        break;
}

1
投票

正如奥马尔·杰克曼(Omar Jackman)已经指出的那样,缺少必要的

beak
关键字。此外,要处理无效输入(除了 'D'、'd'、'E'、'e'、'T'、't' 之外的字母,请使用关键字
default
):

switch(case)
    case 'D':
    case 'd': 
         double dollar = (amount/18*10);
         System.out.println(amount + " Turkish Lira(s) --> " + dollar + " Dollar");
         break;

    case 'E':
    case 'e':
         double euro = (amount/23*10);
         System.out.println(amount + " --> " + euro + " Euro")
         break;

    //.. rest of your code
    // finally:

    default: // <- handle invalid letter input
         System.out.println("Invalid input");
         break;
}

0
投票

我认为问题不仅在于缺少

break
,而且逻辑流程也不正确。
switch
语句应位于
while
循环内。

boolean done = false; 
while (!done){
   done = true;
   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");
         break;
      case 'E':
      case 'e':
         double euro = (amount/23*10);
         System.out.println(amount + " --> " + euro + " Euro");
         break;
      case 'T':
      case 't':
         double lira = (amount);
         System.out.println(amount + " --> " + lira+ " Lira(s)");
         break;
       default: 
         System.out.println(detect + " is not unknown\n");
         done = false;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.