我需要创建一个Java日期测试程序

时间:2022-10-30 08:55:43

I'm fairly new to Java and working on a program called GoodDate, where the program outputs YES or NO according to whether the argument is a valid date (using args in terminal). Since there are many possibilities, I've been briefed thanks to the wonderful internet that I should use a switch here, but I'm having trouble getting started. Any help for the n00b would be appreciated! I also already have a leap year program I could wrap into a class, here it is:

我是Java的新手,正在开发一个名为GoodDate的程序,程序根据参数是否为有效日期(使用终端中的args)输出YES或NO。由于有很多可能性,我已经得到了简要的介绍,感谢我在这里使用开关的精彩互联网,但我开始时遇到了麻烦。任何有关n00b的帮助将不胜感激!我也已经有了一个闰年程序,我可以将它包装成一个类,这里​​是:

public class LeapYear {
    public static void main(String[] args) {
        int year = Integer.parseInt(args[0]);
        java.lang.String isLeapYear;

        isLeapYear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? "YES"
                : "NO");

        System.out.println(isLeapYear);
    }
}

1 个解决方案

#1


0  

I'd make isLeapYear a boolean - true or false. That's what that type is for. Why would you make it a String?

我将isLeapYear作为布尔值 - true或false。这就是那种类型。你为什么要把它变成一个字符串?

I'd recommend moving that into a function on its own so you can reuse it:

我建议将它移动到一个函数中,以便您可以重用它:

public class DateUtils {

    public static void main(String [] args) {
        for (String arg : args) {
            // operate on arguments here.
        }
    }

    public static boolean isLeapYear(Date date) {
       return false;  // put your logic here.
    }
}

I don't see a single reference to a java.util.Date class. You're doing yourself a disservice by not leveraging it.

我没有看到对java.util.Date类的单个引用。不利用它,你自己也是一种伤害。

The rest of your requirement is too vague to comment on.

你的其他要求太模糊,无法发表评论。

#1


0  

I'd make isLeapYear a boolean - true or false. That's what that type is for. Why would you make it a String?

我将isLeapYear作为布尔值 - true或false。这就是那种类型。你为什么要把它变成一个字符串?

I'd recommend moving that into a function on its own so you can reuse it:

我建议将它移动到一个函数中,以便您可以重用它:

public class DateUtils {

    public static void main(String [] args) {
        for (String arg : args) {
            // operate on arguments here.
        }
    }

    public static boolean isLeapYear(Date date) {
       return false;  // put your logic here.
    }
}

I don't see a single reference to a java.util.Date class. You're doing yourself a disservice by not leveraging it.

我没有看到对java.util.Date类的单个引用。不利用它,你自己也是一种伤害。

The rest of your requirement is too vague to comment on.

你的其他要求太模糊,无法发表评论。