计算复活节星期天的日期 [英] Calculate the date of Easter Sunday

查看:159
本文介绍了计算复活节星期天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来计算复活节星期天的日期.复活节星期日是春天的第一个满月之后的第一个星期日. 使用数学家卡尔·弗里德里希·高斯(Carl Friedrich Gauss)在1800年发明的算法:

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 Carl Friedrich Gauss in 1800:

  1. y为年份(例如1800或2001)
  2. y除以19,然后调用其余的a.忽略商.
  3. y除以100得到商b和余数c.
  4. b除以4以获得商d和余数e.
  5. 8 * b + 13除以25以得到商g.忽略其余部分.
  6. 19 * a + b - d - g + 15除以30以得到余数h.忽视 商.
  7. c除以4得到商j和余数k.
  8. a + 11 * h除以319以获得商m.忽略其余部分.
  9. 2 * e + 2 * j - k - h + m + 32除以7以得到余数r.忽略商.
  10. h - m + r + 90除以25以得到商n.忽略 其余的.
  11. h - m + r + n + 19除以32以得到p的余数.忽略 商.
  1. Let y be the year (such as 1800 or 2001)
  2. Divide y by 19 and call the remainder a. Ignore the quotient.
  3. Divide y by 100 to get a quotient b and a remainder c.
  4. Divide b by 4 to get a quotient d and a remainder e.
  5. Divide 8 * b + 13 by 25 to get a quotient g. Ignore the remainder.
  6. Divide 19 * a + b - d - g + 15 by 30 to get a remainder h. Ignore the quotient.
  7. Divide c by 4 to get a quotient j and a remainder k.
  8. Divide a + 11 * h by 319 to get a quotient m. Ignore the remainder.
  9. Divide 2 * e + 2 * j - k - h + m + 32 by 7 to get a remainder r. Ignore the quotient.
  10. Divide h - m + r + 90 by 25 to get a quotient n. Ignore the remainder.
  11. Divide h - m + r + n + 19 by 32 to get a remainder of p. Ignore the quotient.

然后复活节是n月的第p天.

Then Easter falls on a day p of month n.

例如,如果y为2001:

For example, if y is 2001:

a = 6
b = 20
c = 1
d = 5
e = 0
g = 6
h = 18
j = 0
k = 1
m = 0
r = 6
n = 4
p = 15

因此,在2001年,复活节星期日是4月15日.

Therefore, in 2001, Easter Sunday fell on April 15.

确保提示用户输入年份并让用户输入年份.另外,请确保输出p和n的值以及描述输出值的相应消息.

Make sure you prompt the user for a year and have the user input the year. Also, make sure you output the values of p and n with the appropriate messages describing the values output.

将其放入Java代码时遇到了一些麻烦.这是我尝试过的:

I'm having a little trouble putting this into Java code. Here's what I've tried:

import java.util.Scanner;



public class Easter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int y = 2014;
        int a = y % 19;
        int b = y / 100;
        int c = y % 100;
        int d = b / 4;
        int e = b % 4;
        int g = (8 * b + 13) / 25;
        int h = (19 * a + b - d - g + 15) % 30;
        int j = c / 4;
        int k = c % 4;
        int m = (a + 11 * h) / 319;
        int r = (2 * e + 2 * j - k - h + m + 32) % 7;
        int n = (h - m + r + 90) / 25;
        int p = (h - m + r + n + 19) % 32;

        getEasterSundayMonth = n;
        System.out.println("Month: " + Easter.getEasterSundayMonth());
    }
}

这就是我所拥有的.我不知道如何分配内容,就像我试图使getEasterSundayMonth等于n的值一样,很确定它是不正确的.我从这里去哪里?

This is what I have. I don't know how to assign stuff, like I tried to get getEasterSundayMonth to equal the value of n, pretty sure its not right. Where do I go from here?

推荐答案

尝试一下:

import java.util.Scanner;

class Easter
{
    public static void main(String[] args)
    {
        System.out.print("Please enter a year to calculate Easter Sunday\n>");
        Scanner s = new Scanner(System.in);
        int inputted = getResult(s);
        while(inputted <= 0)
        {
            System.out.print("Expected a positive year. Please try again:\n>");
            inputted = getResult(s);
        }
        System.out.println(getEasterSundayDate(inputted));
    }

    private static int getResult(Scanner s)
    {
        while(!s.hasNextInt())
        {
            System.out.print("Expected a valid year. Please try again:\n>");
            s.nextLine();
        }
        return s.nextInt();
    }

    public static String getEasterSundayDate(int year)
    {
        int a = year % 19,
            b = year / 100,
            c = year % 100,
            d = b / 4,
            e = b % 4,
            g = (8 * b + 13) / 25,
            h = (19 * a + b - d - g + 15) % 30,
            j = c / 4,
            k = c % 4,
            m = (a + 11 * h) / 319,
            r = (2 * e + 2 * j - k - h + m + 32) % 7,
            n = (h - m + r + 90) / 25,
            p = (h - m + r + n + 19) % 32;

        String result;
        switch(n)
        {
            case 1:
                result = "January ";
                break;
            case 2:
                result = "February ";
                break;
            case 3:
                result = "March ";
                break;
            case 4:
                result = "April ";
                break;
            case 5:
                result = "May ";
                break;
            case 6:
                result = "June ";
                break;
            case 7:
                result = "July ";
                break;
            case 8:
                result = "August ";
                break;
            case 9:
                result = "September ";
                break;
            case 10:
                result = "October ";
                break;
            case 11:
                result = "November ";
                break;
            case 12:
                result = "December ";
                break;
            default:
                result = "error";
        }

        return result + p;
    }
}

输入2001会导致April 15作为输出.

这篇关于计算复活节星期天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆