+ 2
I need some assistance with my next project.
My last code "David" is my first ever and I'm quite proud of it. But I'm challenging myself now I want to be able to enter a birth year and the script work out the age. all I have to do is call on the result to print out. I don't know how I can do this. Example: Bailey Born "1995" Age" 21" < //I don't want to have to write the number. I want the actual program to work this out from 1995 to 2017. Any help is appreciated. Thank you. //p.s. Let me know what you think of my "David" project.
3 Réponses
+ 4
first of all i commented on your david code ;)
nice job for your very first code ;)
i dont want to write the code for you but
hints for this new code:
- you need scanner to read input
- ask for year with system out println
- with scanner: read input
- now you have to subtract the user input from the actual year
- print it
if you need help on parts just ask again ;)
hope it helps a bit ;)
+ 3
Here is the code if you want it automatized. If you are beginner you should try something that you can compute (instead of calling built-in java function, because they could confuse you).
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LocalDate date = LocalDate.now(); // Gets todays date
int year = date.getYear(); // gets year and stores it
System.out.println("Enter a year of birth:");
int yearOfBirth = in.nextInt();
System.out.println("You are " + (year - yearOfBirth) + " years old.");
}
}