variable might not have been initialised

▼魔方 西西 提交于 2019-12-11 13:55:58

问题


I'm a relatively new student learning Java programming, but I'd like to ask for some help. The error I'm receiving in my code is stating "variable romanNumeral might not have been initialised."

The intent for this program is for a user to enter a number from 1-39 and then have the appropriate roman numeral value displayed to the user via a dialog box. The code is not complete yet, as I've yet to find a solution to this problem due to the application not letting me compile my code.

Here is the code:

public class exercise4 extends Actor
{
    int userNum;
    public void act() 
    {
        intInput(userNum);
    }    

    public String intInput(int userNum)
    {
        String userInput;
        String romanNumeral;

        userInput = JOptionPane.showInputDialog("Please enter a number to be converted into Roman Numerals.");
        userNum = Integer.parseInt(userInput);

        switch(userNum)
        {
           case 1:  romanNumeral = "I";
                    break;

           case 2:  romanNumeral = "II";
                    break;

           case 3:  romanNumeral = "III";
                    break;

           case 4:  romanNumeral = "IV";
                    break;

           case 5:  romanNumeral = "V";
                    break;

           case 6:  romanNumeral = "VI";
                    break;

           case 7:  romanNumeral = "VII";
                    break;

           case 8:  romanNumeral = "VIII";
                    break;

           case 9:  romanNumeral = "IX";
                    break;

           case 10: romanNumeral = "X";
                    break;

           case 11: romanNumeral = "XI";
                    break;

           case 12: romanNumeral = "XII";
                    break;

           case 13: romanNumeral = "XIII";
                    break;

           case 14: romanNumeral = "XIV";
                    break;

           case 15: romanNumeral = "XV";
                    break;

           case 16: romanNumeral = "XVI";
                    break;

           case 17: romanNumeral = "XVII";
                    break;

           case 18: romanNumeral = "XVIII";
                    break;         

           case 19: romanNumeral = "XIX";
                    break;        

           case 20: romanNumeral = "XX";
                    break;

           case 21: romanNumeral = "XXI";
                    break;

           case 22: romanNumeral = "XXII";
                    break;

           case 23: romanNumeral = "XXIII";
                    break;         

           case 24: romanNumeral = "XXIV";
                    break;

           case 25: romanNumeral = "XXV";
                    break;

           case 26: romanNumeral = "XXVI";
                    break;

           case 27: romanNumeral = "XXVII";
                    break;

           case 28: romanNumeral = "XXVIII";
                    break;       

           case 29: romanNumeral = "XXIX";
                    break;

           case 30: romanNumeral = "XXX";
                    break;

           case 31: romanNumeral = "XXXI";
                    break;

           case 32: romanNumeral = "XXXII";
                    break;

           case 33: romanNumeral = "XXXIII";
                    break;

           case 34: romanNumeral = "XXXIV";
                    break;

           case 35: romanNumeral = "XXXV";
                    break;

           case 36: romanNumeral = "XXXVI";
                    break;

           case 37: romanNumeral = "XXXVII";
                    break;

           case 38: romanNumeral = "XXXVIII";
                    break;

           case 39: romanNumeral = "XXXIX";
                    break;
        }

        return romanNumeral;
    }
}

回答1:


Consider how the code would behave if userNum has a value of 40. The switch statement doesn't have a case that matches such a value, so it would do nothing. Which is what the compiler is complaining about: the variable romanNumeral is not initialized when it's declared, and might not be even after the switch - thus: "variable romanNumeral might not have been initialised."

Two simple fixes: (A) initialize at declaration, e.g. String romanNumeral = "?", or (B) add a default part to the switch, as:

switch(userNum)
{
    // other cases first

    default: romanNumeral = "?";
}



回答2:


use default in your switch case. in java you must have initialize a variable before using it. in your code if there is a value where no case matches then the variable will not be initialized.




回答3:


Add in a default case to your switch statement setting it to some error value. You are getting that warning because it is possible your switch matches none of that and romanNumeral will never get set before it's returned.




回答4:


String romanNumeral makes a reference to a memory location, but doesn't initialize it (doesn't give it a value). Because you can provide a value of usernum that doesn't cause a value to be set for romanNumeral, you're getting an error.
To avoid this, you can add a default case.




回答5:


In Java, variables defined in some method are not automatically initialized. Here you have two options: 1. Initialize it using String romanNumeral = null (or something); 2. Use default in switch default: romanNumeral = null (or something);




回答6:


The error just means that the variable still has no memory allocated in it. so, what you will do to remove the error is just to give it an initial value.This will do :

String romanNumeral = "";




回答7:


It is important to initialize the variable, object instances or any data structure you use. Sometimes it gives the null error but sometimes it does not even give an error and can give wrong values.

Your question have been answered above but I would like to suggest a modification. Following this table, you can make a HashMap of the roman numbers:

Decimal value (v)   Roman numeral (n)
1                   I
4                   IV
5                   V
9                   IX
10                  X
40                  XL
50                  L
90                  XC
100                 C
400                 CD
500                 D
900                 CM
1000                M

Using the hashmap you can calculate the roman number for particular integer.Check the code just to give you an idea to get started:

public static void main(String []args){
     int c = 39;int temp =0;
     String roman = "";

     if(c<40 && C>10)
     {
         temp = c/10;
         c = c%10;
         for(int i=0;i<temp;i++)
         {
            roman = roman+map.get(10);

         }
     }
     if(c<10 && c>5)
     {
       if(c==9)
         {
          roman = roman+map.get(9);
         }else{

             temp = c/5;
             c = c%5;
             if(temp==1)
             roamn += map.get(5);
         for(int i=0;i<c;i++)
         {
            roman = roman+map.get(1);

         }
      //again you will have to check a case for four the way I did for 9       
         }

     }
     }

    System.out.println(roman);
 }


来源:https://stackoverflow.com/questions/36370042/variable-might-not-have-been-initialised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!