2016-08-05 10 views
-1

年齢を表示するにはどのように誕生年と現在の年を差し引くことができますか?Javaスキャナとintには年齢が表示されません

Scanner user_input = new Scanner(System.in); 
    String birth_year; 

    System.out.print("Enter your the year you were born: "); 
    birth_year = user_input.next(); 

    int age, current_year = 2016; 
    age = current_year - birth_year; 

    System.out.println("Your age is "+age); 

答えて

2

使用user_input.nextInt()整数値を読むために、あなたはsubstract文字列ができず、int型:

Scanner user_input = new Scanner(System.in); 
int birth_year; 

System.out.print("Enter your the year you were born: "); 
birth_year = user_input.nextInt(); 

int age, current_year = 2016; 
age = current_year - birth_year; 

System.out.println("Your age is "+age) 
+0

おかげで、このようなものを使用することができます!それは完璧に動作します。 – cookieWarrior

+0

@Zellもし回答があれば、その答えに承認をマークしてください。 –

0

また

LocalDate birthdate = LocalDate.of(YEAR, MONTH, DAY); 
    LocalDate now = LocalDate.now(); 
    int age = Period.between(birthdate, now).getYears(); 
+0

それは私にとって初めてのことです!知識を共有してくれてありがとう。私はコーディングが初めてです! – cookieWarrior

関連する問題