变量可能尚未初始化 [英] variable might not have been initialised

查看:133
本文介绍了变量可能尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个学习Java编程的相对较新的学生,但是我想寻求帮助。我在代码中收到的错误表明变量romanNumeral可能尚未初始化。

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."

此程序的目的是为了用户输入1到39之间的数字,然后通过对话框向用户显示适当的罗马数字值。代码尚未完成,因为由于应用程序不允许我编译我的代码,因此我尚未找到解决此问题的方法。

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.

这里是代码:

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;
    }
}


推荐答案

考虑如果 userNum 的值为40,代码的行为。 switch 语句没有 case 匹配这样的值,因此它什么也不做。编译器抱怨的是:变量 romanNumeral 在声明时未初始化,而可能在<$ c $之后甚至没有被初始化。 c> switch -因此:变量romanNumeral可能尚未初始化。

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."

两个简单的解决方法:(A)在声明时初始化,例如 String romanNumeral =? ,或(B)将默认部分添加到开关,如下:

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 = "?";
}

这篇关于变量可能尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆