简体   繁体   English

java切换大小写无法解析为变量

[英]java switch case can not resolve to variable

I have a simple switch case case. 我有一个简单的switch case盒。

public static int setMapTile() {

    int a = getArmadaLength(); // 4 to 6 
    int b;
    switch (a) {

        case 4:
            System.out.println(" recommended MapSize : 10 x 10");
            b = setSize();// method for bigger map, return int

            break;
        case 5:
            System.out.println(" recommended MapSize : 11 x 11");
            b = setSize();// method for bigger map, return int

            break;
        case 6:
            System.out.println(" recommended MapSize : 12 x 12");
            b = setSize();// method for bigger map, return int

            break;            
        default:
            System.out.println("wrong"); // even though it is impossible!
            break;
    }

    return b;
}

It says that b might not have been initialized. 它说b可能尚未初始化。 Do I have to use setter & getter to assign the chosen value to b ? 我是否必须使用settergetter将选定的值分配给b

No you do not need a setter. 不,您不需要二传手。 Simply initialize the variable: int b = 0; 只需初始化变量: int b = 0;

Yes you didn't initialized b . 是的,您没有初始化b Set some default value as per your requirement, say, int b=-1; 根据您的要求设置一些默认值,例如int b=-1;

As the exception states, you never initialize b by assigning it a value if your switch should run its default case. 例外情况是,如果您的开关应运行default情况,则永远不要通过为b赋值来初始化 b。

int b; merely declares it. 声明它。

to fix your issue, you can simply change it to int b = 0; 要解决您的问题,只需将其更改为int b = 0;

The JVM will look at all possible outcomes. JVM将查看所有可能的结果。 One of them is: 其中之一是:

default:
            System.out.println("wrong"); // even though it is impossible!
            break;

after which, you return b. 之后,您将返回b。 But, since b is a local variable, it has no default value. 但是,由于b是局部变量,因此它没有默认值。 You'll need to initialize it for all the possibilities, including the default one: 您需要针对所有可能的情况对其进行初始化,包括默认的情况:

default:
            System.out.println("wrong"); // even though it is impossible!
            b = 0;
            break;

Or give ba value on declaration: 或在声明中给出ba值:

int b = 0;

At the point of return b , b should be initialised. return breturn bb应该被初始化。 It will be initialised in case a is 4 , 5 and 6 ; 它的情况下将初始化a456 ; but what if a is 28 ? 但是如果a28怎么办? You're saying it shouldn't be able to happen, but things that shouldn't happen happen all the time, and the compiler likes to have all its bases covered. 您是说它不应该发生,但是不应该发生的事情一直在发生,并且编译器希望覆盖所有基础。

Either initialise b at top to cover all cases, or make sure to assign it in each branch of the switch (including default ). 可以在顶部初始化b来覆盖所有情况,或者确保将其分配到switch每个分支中(包括default )。

Another possibility (which I actually like best) is to make sure your code can not reach return in the default case; 另一种可能性(我实际上最喜欢)是确保您的代码在默认情况下无法return you can throw an exception there (maybe IllegalStateException , "there is something wrong with the code!") and the compiler will then know it can't reach return through default , and not complain about b being unassigned. 您可以在该处引发异常(也许是IllegalStateException ,“代码有问题!”),然后编译器将知道它无法通过default到达return ,并且不会抱怨b未分配。 Your code should not happily proceed when your assumptions are violated, so this fits well. 违反您的假设时,您的代码不应愉快地进行,因此非常合适。

public static int setMapTile() {
    int a = getArmadaLength(); // 4 to 6 
    int b;
    switch (a) {
        case 4:
            System.out.println(" recommended MapSize : 10 x 10");
            b = setSize();// method for bigger map, return int
            break;
        case 5:
            System.out.println(" recommended MapSize : 11 x 11");
            b = setSize();// method for bigger map, return int
            break;
        case 6:
            System.out.println(" recommended MapSize : 12 x 12");
            b = setSize();// method for bigger map, return int
            break;            
        default:
            throw new IllegalStateException("Armada length is " + a + "?!?");
            break;
    }
    return b;
}

In this specific case, actually, you can even factor out the b outside of the switch and completely avoid the problem. 实际上,在这种情况下,您甚至可以将b排除在switch之外,从而完全避免出现此问题。 Also, that last break is redundant. 同样,最后的break是多余的。

public static int setMapTile() {
    int a = getArmadaLength(); // 4 to 6 
    switch (a) {
        case 4:
            System.out.println(" recommended MapSize : 10 x 10");
            break;
        case 5:
            System.out.println(" recommended MapSize : 11 x 11");
            break;
        case 6:
            System.out.println(" recommended MapSize : 12 x 12");
            break;            
        default:
            throw new IllegalStateException("Armada length is " + a + "?!?");
    }
    return setSize();
}

You need to assign a value to b , when you declare it or in the default block. 声明时或在default块中,需要为b分配一个值。

Instance fields have implicit default values when declared, but variables in methods do not. 实例字段在声明时具有隐式默认值,但方法中的变量则没有。

public class Foo {
    private int age; // this defaults to 0

    public void bar() {
        int height; // this won't compile unless you follow it with an assignment
    }
}

The Java compiler doesn't know that a can only be 4 to 6, so it considers the possibility of the default case. Java编译器不知道a只能是4到6,因此它考虑了默认情况的可能性。 So if the default case of the switch is taken, then, b was never initialized when you do return b . 因此,如果采用开关的默认大小写,则在return b时,b从未被初始化。

There is chance that non of your cases in the switch gets executed and if that is the case, it ll come to the default block. switch中的所有情况都有可能被执行,如果是这种情况,它将进入default块。 In the default block, you are not setting the value of b . default块中,您未设置b的值。 You never initialized b in the first place. 您从来没有初始化b That is why this is happening. 这就是为什么发生这种情况。

To avoid this situation, you can do one of the followings : 为了避免这种情况,您可以执行以下任一操作:

  1. int b = -1; // Initialize b

2. 2。

default: {
            System.out.println("wrong"); // even though it is impossible!
            b = -1;
            break;
          }

Either change this line: int b; 更改此行: int b; to int b = 0; int b = 0;

Or change your code to this shorter variant: 或将代码更改为以下较短的变体:

public static int setMapTile() {

    int a = getArmadaLength(); // 4 to 6 
    switch (a) {

        case 4:
            System.out.println(" recommended MapSize : 10 x 10");
            return setSize();// method for bigger map, return int
        case 5:
            System.out.println(" recommended MapSize : 11 x 11");
            return setSize();// method for bigger map, return int
        case 6:
            System.out.println(" recommended MapSize : 12 x 12");
            return setSize();// method for bigger map, return int           
        default:
            System.out.println("wrong"); // even though it is impossible!
    }

    return 0;
}

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

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