2015-01-26 11 views
1

Java 클래스에 대해이 프로그램을 컴파일하고 실행하려고합니다. 컴파일 할 때마다 변수 연도가 초기화되지 않았을 수 있다고합니다. 사용자가 birthyear로 입력 한 내용을 기반으로 결정되었으므로 초기화 할 수 없습니다. 아무도 내가 잘못 가고 있는지 알 수 있습니까? 감사. (또한 : 모든 데이터 형식을 문자열로 변경했습니다. 정수로 사용하면 "호환되지 않는 형식 : 문자열을 int로 변환 할 수 없기 때문에"라고합니다.)변수 연도가 초기화되지 않았을 수도 있음

import java.util.Scanner; 

public class Hundred 
{ 
    public static void main(String[] args) 
    { 
    Scanner stdIn = new Scanner(System.in); 
    String month; 
    String day; 
    String year; 
    String HundredthBirthday = ((year) + 100); 


    System.out.print("Enter the month in which you were born "); 
    month = stdIn.nextLine(); 

    System.out.print("Enter the day of the month on which you were born: "); 
    day = stdIn.nextLine(); 

    System.out.print("Enter the year that you were born "); 
    year = stdIn.nextLine(); 


    System.out.println("Your Hundredth birtday will be on" + month + day + HundredthBirthday + "!"); 
    } // end main 
} // end class Hundred 

는 의견을 당신에게 모두 감사합니다 여기에

코드입니다! 왜 내가 전에 이것을 깨닫지 못했는지 모르겠다. 사용자가 연도를 입력하기 전에 "Hirththirthday"를 계산하려고 시도하는 것이 분명히 어리석은 일이었습니다. 또한 특정 변수에 대해 "stdIn.nextInt"를 사용해야하는 곳에 "stdIn.nextLine"을 사용하고 있다는 것을 알게되었습니다. 그게 내 "문자열을 int로 변환 할 수 없다"오류가 발생하는 곳입니다. 여기

내가 그 일을 결국 방법은 다음과 같습니다 당신이 그들을 초기화 할 때

import java.util.Scanner; 

public class Hundred 
{ 
    public static void main(String[] args) 
    { 
    Scanner stdIn = new Scanner(System.in); 
    String month; 
    int day; 
    int Year; 

    System.out.print("Enter the month in which you were born: "); 
    month = stdIn.nextLine(); 

    System.out.print("Enter the day of the month on which you were born: "); 
    day = stdIn.nextInt(); 

    System.out.print("Enter the year that you were born: "); 
    Year = stdIn.nextInt(); 

int HundredthBirthday = (Year + 100); 

    System.out.println("Your Hundredth birtday will be on " + month +" " + day + " " + HundredthBirthday + "!"); 
    } // end main 
} // end class Hundred 
+3

'((year) + 100)'에, 아직 존재하지 않는 것에'100 '을 어떻게 추가 할 예정입니까? – August

+0

'() -> ((Integer.parseInt (year)) + 100)'과 같은 어떤 람다 표현식이 도움이 될 수 있습니다. 나중에 평가할 수 있습니다.'year'의 문자열 표현을 숫자 값 .... ""2015 "+"100 "'은"2015100 "이며 찾고자하는 값은 아닙니다. –

답변

0

는 변수를 정의합니다. 또한 year을 얻을 때까지 hundredthBirthday을 할당 할 수 없습니다!

// String month; 
// String day; 
// String year; 
System.out.print("Enter the month in which you were born "); 
String month = stdIn.nextLine(); 
System.out.print("Enter the day of the month on which you were born: "); 
String day = stdIn.nextLine(); 
System.out.print("Enter the year that you were born "); 
String year = stdIn.nextLine(); 
// Presumably, you want a number! 
int hundredthBirthday = (Integer.parseInt(year) + 100); 

또는, 처음 (같은)

System.out.print("Enter the month in which you were born "); 
int month = stdIn.nextInt(); 
System.out.print("Enter the day of the month on which you were born: "); 
int day = stdIn.nextInt(); 
System.out.print("Enter the year that you were born "); 
int year = stdIn.nextInt(); 
int hundredthBirthday = year + 100; 
+1

분명히 예제 코드이지만 @Fryguy는'year'의 값을 검사하여'parseInt'에 전달하기 전에 유효한 정수를 나타내는 지 확인하는 것이 좋습니다. 그렇지 않으면'NumberFormatException'이 발생할 수 있습니다. –

+0

@BobJarvis 좋은 지적; 'nextInt()'를 사용하여 대안을 추가했습니다. –

+0

공정하게 말하면,'nextInt' 역시 똑같이 문제가됩니다. 조작 된 입력에 대해서도 예외가 발생합니다. –

-1

지역 변수 (메소드 내부 변수)에 int 같은 year, monthday를 얻을이 초기화하지 않고 사용할 수 없습니다 . 기본값은 null이어야합니다.

변수를 인스턴스 변수로 설정하여이 작업을 수행 할 수도 있습니다.

관련 문제