"error: variable might not have been initiailzed"



在家里做一些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");
   }    
  }     

这是一个有效的错误,在您的最后一个else中您没有设置String season

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

有不初始化season的情况

相关内容

最新更新