简体   繁体   中英

Initialize variable inside if statement (Java)

I'm assigning my variables in an if statement and trying to use them outside of it, but I can't figure out how. The error is in the very last if statement stating that "local variable may not have been initialized".

int i,z;
if (st1.nextToken() == "Ace")
{
    String Ace = "14";
    i = Integer.parseInt(Ace);  
}
else if (st2.nextToken() == "Ace")
{
    String Ace = "14";
    z = Integer.parseInt(Ace);
}
else if (st1.nextToken() == "King")
{
    String King = "13";
    i = Integer.parseInt(King); 
}
else if (st2.nextToken() == "King")
{
    String King = "13";
    z = Integer.parseInt(King); 
}
else if (st1.nextToken() == "Queen")
{
    String Queen= "12";
    i = Integer.parseInt(Queen);
}
else if (st2.nextToken() == "Queen")
{
    String Queen= "12";
    z = Integer.parseInt(Queen);
}
else if (st1.nextToken() == "Jack")
{
    String Jack = "11";
    i = Integer.parseInt(Jack);
}
else if (st2.nextToken() == "Jack")
{
    String Jack = "11";
    z = Integer.parseInt(Jack); 
    break;
}
else
{
    i = Integer.parseInt(st1.nextToken());  
    z = Integer.parseInt(st2.nextToken());
}
if (i > z)

Replace:

int i,z;

with:

int i=0, z=0;  //or whatever value you want

Or initialize both i and z to some value in all of your if-else blocks.

You cannot do if(i>z) unless both are initialized because you need to initialize local variables.

The compiler must be satisfied that both i and z have been initialised before it will permit the comparison i > z . Your code does not guarantee this as only some paths through your if..else logic will initialise both variables.

You have a couple of options:

  • Initiliase i and z at the very beginning, eg. to 0 or some other suitable default
  • Make sure you set i and z in every if block

A couple of other style issues:

  • Use equals() to compare strings, not == , eg. st1.nextToken().equals("Ace")
  • Assigning a string value (containing a number) to a string and then parsing it for the integer is inefficient. You can simple assign an integer value.

Thus:

String Jack = "11";
i = Integer.parseInt(Jack);

can become:

i = 11;

or:

i = JACK;   // where JACK is a constant set to 11

You may also have a bug with respect to the tokens being read. Each if expression calls nextToken() which will cause the next token in the stream to be used. I imagine you want to compare the current token in each expression. If this is the case, then assign to two temporary variables to hold the next tokens for st1 and st2 before your if..else logic and compare these to Ace, King, etc.

在这种情况下,针对String比较的一个更优雅的解决方案是使用switch / case语句

I suggest using Integer class. If none of the conditions before last if statement satisfy, put a check if those Integers are initialized with null check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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