Calculate the date of Easter Sunday

后端 未结 6 605
半阙折子戏
半阙折子戏 2020-12-11 16:32

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

6条回答
  •  春和景丽
    2020-12-11 16:51

    You aren't far from getting your program working. You really have two things left you need to do.

    • Prompt the user for a year
    • Output the date found

    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);
    

提交回复
热议问题