Write a program to compute the date of Easter Sunday. Easter Sunday is the first Sunday after the first full moon of spring. Use the algorithm invented by the mathematician
You aren't far from getting your program working. You really have two things left you need to do.
The trick to using a Scanner to prompt the user for input is to create a while-loop which tests each line the user enters, and keeps repeating until it sees a legal value.
Instead of hard-coding y = 2014;
(or whatever), you want to do something like this:
Scanner input = new Scanner(System.in);
int y = -1; // No easter back in B.C.
while (y < 0) {
System.out.println("Please enter a year (integer greater than zero)");
if (input.hasNextInt()) { // check to see if the user entered a number
y = input.nextInt(); // if so, read it
}
input.nextLine(); // advance the scanner to the next line of input
}
in this case, each time the user doesn't enter a number, y
remains -1
and the loop continues.
You are already doing all the calculations correctly, so to end your program, you just need to output the month/day.
I wouldn't bother trying to extract the calculation into a helper method. Just use the calculated values directly in main()
:
int a = y % 19;
int b = y / 100;
...
int n = (h - m + r + 90) / 25;
int p = (h - m + r + n + 19) % 32;
System.out.println("In the year " + y + " Easter with fall on day " + p + " of month " + n);