2016-08-18 6 views
0

코드 마지막 부분에서 변수 초기화 오류가 발생합니다. 코드의 나머지 부분과 형식이 다르지 않기 때문에 이상하게 느껴집니다. 여러 번 검토 한 결과 문제가 보이지 않습니다. 나는 이것에 대해 또 다른 시각이 필요하다고 생각한다.나머지 코드와 정확히 동일한 코드 마지막 부분의 오류

아이디어 누구?

import java.util.Scanner; 
public class Survey 
{ 
public static void main(String[] args) 
{ 
    int surveyChoice; 
    String choice; 
    String choice2; 
    String choice3; 
    String choice4; 
    final int FIRST_CHOICE = 1; 
    final int SECOND_CHOICE = 2; 
    final int THIRD_CHOICE = 3; 
    final int FOURTH_CHOICE = 4; 
    final int FIFTH_CHOICE = 5; 
    final int SIXTH_CHOICE = 6; 
    final int SEVENTH_CHOICE = 7; 
    final int EIGHTH_CHOICE = 8; 
    final String FIRST_PICK = "Love"; 
    final String SECOND_PICK = "Money"; 
    final String THIRD_PICK = "Long Safe Life"; 
    final String FOURTH_PICK = "Short Fun Life"; 
    final String FIFTH_PICK = "Few Very Close and Trusted Friends"; 
    final String SIXTH_PICK = "Internationaly Fameous"; 
    final String SEVENTH_PICK = "Pass away comfortably while sleeping"; 
    final String EIGHTH_PICK = "Quick Sudden Death doing what you love"; 
    String message; 
    String message2; 
    String message3; 
    String message4; 

    Scanner input = new Scanner(System.in); 
    System.out.println("What would you rather have?"); 
    System.out.println("Enter 1 for Love or 2 for Money."); 
    surveyChoice = input.nextInt(); 

    if(surveyChoice == 1) 
    { 
    choice = FIRST_PICK; 
    message = "Love"; 
    } 
    else if(surveyChoice == 2) 
    { 
    choice = SECOND_PICK; 
    message = "Money"; 
    } 
    else 
    choice = "invalid"; 
    message = "An invalid option"; 

    System.out.println("How would you rather live?"); 
    System.out.println("Enter 3 for Long Safe Life or 4 for Short Fun Life."); 
    surveyChoice = input.nextInt(); 

    if(surveyChoice == 3) 
    { 
    choice2 = THIRD_PICK; 
    message2 = "Long Safe Life"; 
    } 
    else if(surveyChoice == 4) 
    { 
    choice2 = FOURTH_PICK; 
    message2 = "Short Fun Life"; 
    } 
    else 
    choice2 = "invalid"; 
    message2 = "An invalid option"; 


    System.out.println("What kind of relationships would you rather have?"); 
    System.out.println("Enter 5 for Few Very Close and Trusted Friends or 6 for Internationaly Fameous."); 
    surveyChoice = input.nextInt(); 

    if(surveyChoice == 5) 
    { 
    choice3 = FIFTH_PICK; 
    message3 = "Few Very Close and Trusted Friends"; 
    } 
    else if(surveyChoice == 6) 
    { 
    choice3 = SIXTH_PICK; 
    message3 = "Internationaly Fameous"; 
    } 
    else 
    choice3 = "invalid"; 
    message3 = "An invalid option"; 


    System.out.println("How would you rather die?"); 
    System.out.println("Enter 7 for Pass away comfortably while sleeping or 8 for Quick Sudden Death doing what you love."); 
    surveyChoice = input.nextInt(); 

    if(surveyChoice == 7) 
    { 
    choice4 = SEVENTH_PICK; 
    message4 = "Pass away comfortably while sleeping"; 
    } 
    else if(surveyChoice == 8) 
    { 
    choice4 = EIGHTH_PICK; 
    message4 = "Quick Sudden Death doing what you love"; 
    } 
    else 
    choice = "invalid"; 
    message = "An invalid option"; 

    System.out.println("You want " + message); 
    System.out.println("You want " + message2); 
    System.out.println("You want " + message3); 
    System.out.println("You want " + message4); 
    System.out.println("You will have " + choice); 
    System.out.println("You will have " + choice2); 
    System.out.println("You will have " + choice3); 
    System.out.println("You will have " + choice4);  
} 

} 
+1

오류가 무엇입니까? 오류가있는 행을 식별하는 스택 추적이 있습니까? 오류를 나타내는 _minimum_ 코드는 무엇입니까? –

+0

Survey.java:110 : 오류 : 변수 message4가 초기화되지 않았을 수 있습니다. System.out.println ("원하는"+ message4); –

+0

Survey.java:114 : 오류 : choice4 변수가 초기화되지 않았을 수 있습니다. System.out.println (""+ choice4); –

답변

0

@OpiesDad가 지적했듯이 최종 블록의 변수 이름이 일치하지 않습니다.

또한 내게 가장 먼저 튀어 나온 점은 각 if-then-else 블록에서 마지막 else 블록 주위에 대괄호가 누락되어 해당 메시지 변수가 항상 "유효하지 않음"으로 설정된다는 것입니다 선택권".

과 같아야 다른 마지막 :

else 
{ 
    choice = "invalid"; 
    message = "An invalid option"; 
} 
+0

나는 그것을 할 것이다. 나는 다른 오류 때문에 나에게 던져지지 않은 것 같아요. 감사. –