If-else working, switch not

2019-01-12 00:02发布

I am making an app that has a grid of images with text and each one opens a different activity. It works fine but just for design purposes I want to replace my if-else statements with switch statements (which I assume I can do) however it doesn't work. Right now my working code to set the label on each image is:

if(position == 0)
        textView.setText(R.string.zero);
    else if(position == 1)
        textView.setText(R.string.one);
    else if(position == 2)
        textView.setText(R.string.two);
    else if(position == 3)
        textView.setText(R.string.three);
    else if(position == 4)
        textView.setText(R.string.four);
    else if(position == 5)
        textView.setText(R.string.five);
ect....

I want to use:

switch(position)
case 0:
   textView.setText(R.string.zero);    
case 1:
   textView.setText(R.string.one);
case 2:
   textView.setText(R.string.two);    
case 3:
   textView.setText(R.string.three);
case 4:
   textView.setText(R.string.four);    

but when I did that ever label was the last one that I defined (in my example it would be "four"). I also have a similar code for each object to start a different intent with the position variable however that does the opposite and makes every intent equal to the first one. Is my syntax wrong or will this not work for my situation?

9条回答
家丑人穷心不美
2楼-- · 2019-01-12 00:15

You need to break; after each branch:

switch (position) {
    case 0:
        textView.setText(R.string.zero);
        break; // <-- here
    // etc
}

Legitimate uses of switch when you don't break exist, those are called fall throughs; or because you return or throw.:

switch (someNumber) {
    case 0:
        return 0; 
        // no need for break here
    case 1:
        throw new IllegalArgumentException();
        // no need to break here
    case 2:
        System.out.println("Oh, I got two!");
        // fall through
    case 3:
        return 3;
    default:
        System.out.println("Meh")
        // No need to break: last possible branch
}

return -1;

will return 3 even if you enter 2.

But otherwise you need to break.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-12 00:16

This is the solution. You need to use break to avoid going through each case:

switch(position)
case 0:
   textView.setText(R.string.zero);    
   break;
case 1:
   textView.setText(R.string.one);
   break;
case 2:
   textView.setText(R.string.two);  
   break;  
case 3:
   textView.setText(R.string.three);
   break;
case 4:
   textView.setText(R.string.four);    
   break;

I would recommend to read the oracle documentation about the switch statement.

查看更多
Juvenile、少年°
4楼-- · 2019-01-12 00:18

You need to break; after each statement in a case, otherwise execution flows down (all cases below the one you want will also get called), so you'll always get the last case.

switch(position) {
case 0:
    textView.setText(R.string.zero); 
    break; 
case 1:
    textView.setText(R.string.one);
    break; 
case 2:
    textView.setText(R.string.two);   
    break;  
case 3:
    textView.setText(R.string.three);
    break; 
case 4:
    textView.setText(R.string.four); 
    break; 
}

Here's the official tutorial explaining when to and when not to use break;.

查看更多
混吃等死
5楼-- · 2019-01-12 00:19

In the Switch-case statements, you need to put break; after each case.

switch(position){
case 0:
   textView.setText(R.string.zero);    
   break;
case 1:
  textView.setText(R.string.one);
  break;
case 2:
  textView.setText(R.string.two);    
  break;
case 3:
  textView.setText(R.string.three);
  break;
case 4:
  textView.setText(R.string.four);  
  break;
default:
    System.out.println("not available");
}

Also you need to put default: at last, because when all case are wrong that time perform default: action.

In the switch-case statement not forgot about break; and default action.

查看更多
何必那么认真
6楼-- · 2019-01-12 00:24

The switch needs a break with in each case. But in your case it could be done much simpler by defining an array as shown below.

String values = {R.string.zero, R.string.one, R.string.two, ... };

Use this to populate textView : textView.setText(values[position]);

查看更多
家丑人穷心不美
7楼-- · 2019-01-12 00:25

Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

Switch is faster than if-else statement

Bottom line : Default is optional(works like else statement in switch), Break is mandatory.

Interesting fact: you won't see any compilation error, even if you forgot to place the break statement.

查看更多
登录 后发表回答