简体   繁体   English

如果 - 其他工作,切换不

[英]If-else working, switch not

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. 它工作正常,但出于设计目的,我想用switch statements替换我的if-else statements switch statements (我假设我可以做)但是它不起作用。 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. 我也有一个类似的代码,每个对象用position变量开始一个不同的intent但是相反,并使每个意图等于第一个。 Is my syntax wrong or will this not work for my situation? 我的语法错了还是不适用于我的情况?

You need to break; 你需要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. 在一个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; 这是官方教程,解释何时何时不使用break; .

You need to break; 你需要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; 当你不break时合法使用switch ,这些被称为坠落; or because you return or throw .: 或因为你returnthrow

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. 即使你输入2也会返回3。

But otherwise you need to break . 但否则你需要break

Using a break statement after each case should fix the problem. 在每个案例之后使用break语句应该解决问题。 I would also use a default statement as well after the last case. 在最后一种情况之后我也会使用默认语句。

This is the solution. 这是解决方案。 You need to use break to avoid going through each case: 您需要使用break来避免遍历每个案例:

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 . 我建议阅读有关switch语句的oracle文档。

You need to use break statement after eace case operations. 在eace案例操作之后,您需要使用break语句。 In a switch-case statement if you dont use a break statement then all the cases after that specific one will be executed also 在switch-case语句中,如果你不使用break语句,那么在该特定语句之后的所有情况也将被执行

case 0:
   textView.setText(R.string.zero);    
   break;

Don't forget to put break; 别忘了break; after each case: like that: 在每个案例之后:像那样:

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

In the Switch-case statements, you need to put break; Switch-case语句中,你需要放弃; 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. 你还需要设置默认值:最后,因为当所有情况都错误时,执行默认值:action。

In the switch-case statement not forgot about break; switch-case声明中没有忘记休息; and default action. 和默认操作。

Each break statement terminates the enclosing switch statement. 每个break语句都会终止封闭的switch语句。 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. break语句是必需的,因为没有它们,switch块中的语句都会失败:匹配的case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式,直到遇到break语句。

Switch is faster than if-else statement Switch比if-else语句更快

Bottom line : Default is optional(works like else statement in switch), Break is mandatory. 底线:默认是可选的(与switch中的else语句一样),Break是必需的。

Interesting fact: you won't see any compilation error, even if you forgot to place the break statement. 有趣的事实:即使您忘记放置break语句,也不会看到任何编译错误。

The switch needs a break with in each case. 在每种情况下, switch需要break 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]); 使用它来填充textView: textView.setText(values[position]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM