java

时间:2018-01-29 04:08:25

标签: java compiler-errors formatting syntax-error

全新的java所以我在这个java类的介绍中苦苦挣扎。尽我所能使这个程序工作,但它只是没有发生,我不知道为什么。现在遇到一个错误,它告诉我它找不到符号?

我已经花了这么多时间,我觉得我越来越近了。非常感谢您的帮助。

这是作业:

编写一个程序DayOfWeek.java,它从命令行参数中获取日期作为输入,并打印日期所在的星期几。您的程序应该采用三个命令行参数:m(月),d(日)和y(年)。 1月份使用1次,2月份使用2次,依此类推。输出打印0表示星期日,1表示星期一,2表示星期二,依此类推。对于公历,请使用以下公式:

y0 = y - (14 - m) / 12
x = y0 + y0/4 - y0/100 + y0/400
m0 = m + 12 * ((14 - m) / 12) - 2
d0 = (d + x + (31*m0)/ 12) mod 7

例如,一周的哪一天是1953年8月2日?

y = 1953 - 0 = 1953
x = 1953 + 1953/4 - 1953/100 + 1953/400 = 2426
m = 8 + 12*0 - 2 = 6
d = (2 + 2426 + (31*6) / 12) mod 7 = 2443 mod 7 = 0  (Sunday)

并且输出需要看起来像这样:

8 2 1953年落在0。

到目前为止,这是我的代码:

    public class DayOfWeekTest
    {
    public static void main(String[] args)
    {
            int monrh, day, year;
            month = Integer.parseInt(args[0]);
            day = Integer.parseInt(args[1]);
            year = Integer.parseInt(args[2]);

        int y0 = year - (14 - month) / 12;
        System.out.println(y0);
        int x = y0 + y0/4 - y0/100 + y0/400;
        System.out.println(x);
        int m0 = month + 12 * ((14 - month) / 12) - 2;
        System.out.println(m0);
        int d0 = (day + x + (31 * m0)/ 12) % 7;
        System.out.println(d0);

        System.out.println("Falls on a " + d0);

    }
    }

任何帮助都将非常感激,如果你不介意解释我做错什么会更好。我真的想学习这些东西。非常感谢你们。

1 个答案:

答案 0 :(得分:0)

您的代码完全符合预期!

而不是打印y0xm0,而应打印yearmonthday。这将为您提供正确的输出。您的代码应该看起来像这样:

    int y0 = year - (14 - month) / 12;
    int x = y0 + y0/4 - y0/100 + y0/400;
    int m0 = month + 12 * ((14 - month) / 12) - 2;
    int d0 = (day + x + (31 * m0)/ 12) % 7;

    System.out.print( month + " " + day + " " + year + " falls on " + d0 );