2014-07-15 15 views
-2

집에서 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"); 
    }  
    }  

답변

0

올바른 오류입니다. 마지막으로 String season을 설정하지 않았습니다.

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

season을 초기화하지 않은 경우가 있습니다.

이것은 컴파일러가 불평하는 것입니다.

관련 문제