error: variable might not have been initialized & if statement

后端 未结 4 490
一生所求
一生所求 2020-12-04 01:32

So basically, the

 //Black ops 2 Class generator Please help me FIX!!!!!!
    import java.util.Scanner;
    import java.util.Random;
    public class money
         


        
4条回答
  •  失恋的感觉
    2020-12-04 01:55

    I think your problem lies in this if statement: Assuming you get here and weaponType does equal "rifle", it will return and exit your function. You should initialize your primaryWeapon to a default value, i.e. primaryWeapon = "None";

     if (weaponType.equals("Rifle")) {
                            primaryWeapon1 = primaryGen.nextInt(1) +1;
                            if (primaryWeapon1 == 1) {
                                primaryWeapon = MTAR; //*&%&*This is where i initialized it.
                            }
                            return; //<---- remove this
     }
    

    Also complete the if block, if(yes) {...} else {...}.The java compiler will branch out conditional clauses, and will warn/error when attempting to use unintialized variables. For example:

    int b;
    boolean f = true;
    if(f)
        b =1;
    System.out.println(b); //error because no else block
    
    
    //Fixed
    int b; 
    boolean f = true;
    if(f)
    b = 1;
    else
    b= 2;
    System.out.println(b);
    

    --Niru

提交回复
热议问题