user1810113
user1810113

Reputation: 41

Switch statement unexpected result

I wrote this code but it doesn't seem working. When we type d, it does the calculation for dollar but still doing (..what?).

Can you see + detect + in the list part, the part that is wrong?

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);
            } 
    }

Upvotes: 0

Views: 385

Answers (4)

Yunus
Yunus

Reputation: 84

I think the issue is not only the missing break, the flow of the logic also incorrect. The switch statement should be inside the while loop.

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;
    }
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075527

Add a break at the end of each series of statements in each of your case blocks, e.g.:

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

...and so on. That tells the code that you don't want to carry on into the next case.

I recommend reading a good primer on Java. In the comments, MadProgrammer pointed to one tutorial specific to switch, here, but I'd take a step back and do a bit of a review of the basics.

Toward that end, here's an example of a properly-written switch statement:

// 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;
}

Upvotes: 2

Akos K
Akos K

Reputation: 7133

The necessary beak keyword is missing as Omar Jackman already pointed out. In addition, to handle invalid inputs (letters other than 'D', 'd', 'E', 'e', 'T', 't' use the keyword 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;
}

Upvotes: 1

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

You need to add break at the end of each case for your switch statement.

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

Upvotes: 4

Related Questions