“错误:变量可能尚未初始化”

时间:2014-07-15 13:35:22

标签: java

在家里做一些Java练习,我不断收到这段代码的错误。 我想制作一个程序,告诉已输入的月份(以数字形式),但如果数字大于12,则应告诉我们输入的月份无效。

   import java.util.Scanner;

   class SeasonInput {

    public static void main (String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a month (in numbered form)");

    String monthentered = input.nextLine();

    int month = Integer.valueOf(monthentered);



    String season;

    if(month <13)   {

    if(month == 12 || month == 1 || month == 2)
        season = "Winter";

    else if(month == 3 || month == 4 || month == 5)
        season = "Spring"; 

    else if(month == 6 || month == 7 || month == 8)
        season = "Summer"; 

    else if(month == 9 || month == 10 || month == 11)
        season = "Autumn"; 
    System.out.println("The season that occurs during that month is " + season);                                    
                        }

    else

    System.out.println("Enter a valid month");
   }    
  }     

2 个答案:

答案 0 :(得分:0)

您遇到的情况是您没有初始化season

这就是编译器所抱怨的。

答案 1 :(得分:0)

这是一个有效的错误,在您最后的其他情况下,您没有设置String season

String season = null; // <-- give it a null. the error will go away.