怎样在Java的switch语句外部使用switch case内部的变量?

huangapple go评论78阅读模式
英文:

How can i use variable outside switch case within Java switch?

问题

我在 switch 外部定义了变量,并在 switch case 内部使用它们。我想运行一个代码,该代码将获取任何 switch case 的值。这个代码将位于 switch 外部。我已经尝试过,但是出现了 "STATEMENT UNREACHABLE" 的错误。

以下是我尝试过的代码:

BigDecimal firstvalue, secondvalue, calculation;

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        firstvalue = new BigDecimal("45");
        secondvalue = new BigDecimal("23");

        switch (spinner.getSelectedItemPosition()) {

            case 0: {
                calculation = firstvalue.multiply(secondvalue);
                break;
            }
            case 1: {
                calculation = firstvalue.add(secondvalue);
                break;
            }
            case 2: {
                calculation = firstvalue.subtract(secondvalue);
                break;
            }

            // 以上任何一个 case 将应用于 calculation
            // 然后这个位于 switch 外部的程序应该运行
            calculation.multiply(new BigDecimal("30"));

        }
    }
});
英文:

I have variables defined outside switch and used them within switch case. I want to run a code that will take value of any switch case. This code will be outside switch. I tried but i got error like STATEMENT UNREACHABLE.

Here what i've tried :

BigDecimal firstvalue, secondvalue, calculation;


button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {


firstvalue =  new BigDecimal("45");
secondvalue = new BigDecimal("23");



switch (spinner.getSelectedItemPosition()){

case 0:
{
calculation = firstvalue.multiply(secondvalue);                      
break;
}
case 1:
{
calculation = firstvalue.add(secondvalue);
break;
}
case 2:
{
calculation = firstvalue.subtract(secondvalue);
break;
}

// Any of above case will apply for calculation 
// Then this program that is outside switch should run
   
calculation.multiply(new BigDecimal("30"));
               

}
}
});

答案1

得分: 1

你在 switch 语句内部编写了代码行 calculation.multiply(new BigDecimal("30"))。所以它是 case 2 的一部分。

但是因为在这行代码之前你进行了 break,所以这行代码永远不会被执行到(即使是在 case 2 中也是如此)。你应该按照注释中指定的缩进来编写代码,以便更容易地识别此类错误。

英文:

You have written the code line calculation.multiply(new BigDecimal("30")) inside the switch statement itself. So it is part of case 2.

But since you are breaking before the line, that code line is never reached (even for case2). You should really follow indentations as stated in comments to identify such errors easily.

答案2

得分: 0

calculation在switch内被赋值,但它在switch外部被定义(在switch开始之前)。这意味着您可以在switch之后使用它。

您只需要将calculation.multiply往下移动一行,在其中一个}之后。

在定义时为calculation分配一个默认值可能是个好主意,或者可以在switch中使用一个“default”子句。

英文:

calculation is assigned inside the switch, but it is defined outside the switch (before the switch starts). This means you can use it after the switch.

All you need to do is move calculation.multiply one line down, after one of the }

It might be a good thing to assign a default value to calculation, either where it is defined, or using a "default" clause in your switch

huangapple
  • 本文由 发表于 2020年7月25日 18:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63087377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定