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
'((year) + 100)'에, 아직 존재하지 않는 것에'100 '을 어떻게 추가 할 예정입니까? – August
'() -> ((Integer.parseInt (year)) + 100)'과 같은 어떤 람다 표현식이 도움이 될 수 있습니다. 나중에 평가할 수 있습니다.'year'의 문자열 표현을 숫자 값 .... ""2015 "+"100 "'은"2015100 "이며 찾고자하는 값은 아닙니다. –