Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我收到';空';在我的输出中输入特定日期时_Java_Date_Null - Fatal编程技术网

Java 我收到';空';在我的输出中输入特定日期时

Java 我收到';空';在我的输出中输入特定日期时,java,date,null,Java,Date,Null,我的代码编译和运行都很完美,但是当我选择任何星期六的日期时,输出是“null”而不是“SATURDAY”。下面将提供示例以进一步解释此问题 我试图改变我的“getDayOfWeek”方法中的if语句,但我似乎没有解决方案,我也试图从有经验的程序员那里获得帮助,但他们似乎在挣扎,因为java不是他们的主要语言 守则: class MyDate { // properties of date object private int day, month, year; //

我的代码编译和运行都很完美,但是当我选择任何星期六的日期时,输出是“null”而不是“SATURDAY”。下面将提供示例以进一步解释此问题

我试图改变我的“getDayOfWeek”方法中的if语句,但我似乎没有解决方案,我也试图从有经验的程序员那里获得帮助,但他们似乎在挣扎,因为java不是他们的主要语言

守则:

class MyDate {

    // properties of date object
    private int day, month, year;

    // constructor with arguments
    public MyDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public boolean isDateValid() {
        if (month > 12 || month < 1 || day < 1) { // if values exceed 12 or are negative: return false
            return false;
        } else if (year <= 1582 && month <= 10 && day <= 15) { //     starting date
            //   checking
            return false;
        } // for 31 day months: January, March, May, July, August, October, December
        else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

            if (day > 31) {
                return false;
            }

        } // for 30 day months: April, June, September, November
        else if (month == 4 || month == 6 || month == 9 || month == 11) { 

            if (day > 30) {
                return false;
            }

        } // February check
        else if (month == 2) {

            // leap year check for February
            // 29 days in a leap year
            // 28 days in a common year
            if (isLeapYear()) {
                if (day > 29) {
                    return false;
                }

            } else {
                if (day > 28) {
                    return false;
                }
            }
        }
        return true;
    }

    // checks if input year is leap year
    private boolean isLeapYear() {
        if (year % 4 != 0) {
            return false;
        } else if (year % 400 == 0) {
            return true;
        } else if (year % 100 == 0) {
            return false;
        } else {
            return true;
        }
    }

    // method returns the day of MyDate object
    public int getDay() {
        return day;
    }

    // parameter for day to set
    public void setDay(int day) {
        this.day = day;
    }

    // method returns the month of MyDate object
    public int getMonth() {
        return month;
    }

    // parameter for month
    public void setMonth(int month) {
        this.month = month;
    }

    // method returns the year of MyDate object
    public int getYear() {
        return year;
    }

    // parameter for year
    public void setYear(int year) {
        this.year = year;
    }

    // method returns all variables: day/month/year of MyDate object 
    public String toString() {
        return day + "/" + month + "/" + year;
    }
}

public class MyCalendar {

    //enums for days of week
    public static enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,  SATURDAY;
    };

    //enums for month of year
    public static enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
    };

    //enums for week number of month
    public static enum Week {
        FIRST, SECOND, THIRD, FOURTH, FIFTH;
    };

    //to store Date object
    private MyDate date;

    //constructor taking mydate object
    public MyCalendar(MyDate enteredDate) {
        this.date = enteredDate;
    }

    //main method 
    public static void main(String[] args) {

        boolean dateValid = false; //valid date false
        Scanner input = new Scanner(System.in); //scanner for input
        MyDate enteredDate = null;

        //till valid date found
        while (!dateValid) {
            System.out.print("Enter the date as day month year : ");
            //taking input and creating date output
            enteredDate = new MyDate(input.nextInt(), input.nextInt(), input.nextInt());
            //validating date input

            if (enteredDate.isDateValid()) { //if valid
                MyCalendar myCalendar = new MyCalendar(enteredDate); 
                //creating calendar table

                myCalendar.printDateInfo(); //printing date info
                myCalendar.printCalendar(); //printing calendar
                dateValid = true; //setting validate to true 
            } else {
                System.out.println(enteredDate + " is not a valid date, please re-input a valid date: ");
            }
        }

        input.close();
    }

    // returns number of days in current month
    private int getNumberOfDays() {
        int days = 31;
        int month = date.getMonth();
        if (month == 4 || month == 6 || month == 9 || month == 11)
            days = 30;
        return days;
    }

    //print calendar of input month
    public void printCalendar() {
        System.out.println("\n\nThe Calendar of "+Month.values()[date.getMonth()-1]+" "+date.getYear()+" is :");
        int numberOfMonthDays = getNumberOfDays();
        Day firstWeekdayOfMonth = getDayOfWeek(1, date.getMonth(), date.getYear());
        int weekdayIndex = 0;
        System.out.println("Su Mo Tu We Th Fr Sa");
        // The order of days depends on the input of date 
        // to display output of calendar

        for (int day = 0; Day.values()[day] != firstWeekdayOfMonth; day++) {
            System.out.print("   "); // this loop to print the first day in the
            // correct place
            weekdayIndex++;
        }
        for (int day = 1; day <= numberOfMonthDays; day++) {

            if (day < 10) 
                System.out.print(day + " ");
            else
                System.out.print(day);
            weekdayIndex++;
            if (weekdayIndex == 7) {
                weekdayIndex = 0;
                System.out.println();
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }

    //method to print about date information in literal form
    public void printDateInfo() {
        System.out.println(date + " is a " + getDayOfWeek(date.getDay(), date.getMonth(), date.getYear())
        + " located in the " + Week.values()[getWeekOfMonth() - 1] + " week of "
        + Month.values()[date.getMonth() - 1] + " " + date.getYear());
    }

    /*
     * gets day of the week, returns enum type Day
     *
     * Zellar's congruence to calculate the day of week
     * for any given date after October 15 1582
     * (h) = (q+(13*(m+1)/5)+K+(K/4)+(J/4)+5J)%7 ,q- day of month,
     * m- month, k = year of century (year%100), J = (year/100)
     */
    public Day getDayOfWeek(int day, int month, int year) {
        int q = day;
        int m = month;

        if (m < 3) {
            m = 12 + date.getMonth();
            year = year - 1;
        }
        int K = year % 100;
        int J = year / 100;
        //calculating h value
        int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
        Day output = null;
        if (h < Day.values().length && h > 0) {
            output = Day.values()[h - 1]; //getting respective  enum value
        }
        return output; //returning enum value
    }

    // get week number of current date
    public int getWeekOfMonth() {
        int days = date.getDay();
        int weeks = days / 7;
        days = days % 7;
        if (days > 0)
            weeks++;
        return weeks;
    }

}
实际结果:

25/05/2019 is a null located in the FOURTH week of MAY 2019

The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
            1   2   3   4  
5   6   7   8   9   10  11
12  13  14  15  16  17  18
19  20  21  22  23  24  25
26  27  28  29  30  31
其他日期(非星期六)的结果:

指定问题: 输出在星期六当天的所有日期上打印空值,而非星期六的其他日期将获得所需的正确输出。

if(h0){
应改为

if(h<(Day.values().length+1)和&h>0){
  • 如果这是针对生产代码的,则是正确的:您不应该开发自己的
    MyDate
    类,而应该依赖内置的
    LocalDate
  • 如果另一方面,正如我所假设的,这是一个编程练习,这是一个很好的练习,并且没有理由(我可以看到)你不应该挣扎通过它
计算一周中某一天的公式为:

    //calculating h value
    int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
(顺便说一句,请找到更好的变量名,并遵守Java命名约定:变量名不能是大写字母。)我不理解这个公式,但假设它是正确的,它会将一周中的某一天计算为0=周六,1=周日到6=周五。若要使用此数字查找
枚举,请使用

        output = Day.values()[(h + 6) % 7]; //getting respective  enum value
由于
h
始终是非负的且小于7,因此不需要包含
if
语句。只需无条件地分配到
output
。通过这些更改,我可以


您是否已尝试使用调试器逐行运行代码,以查明空值的确切来源?您是否涵盖了getDay()返回的值这一事实是一个与一周中的某一天相对应的整数:0表示星期天,1表示星期一,2表示星期二…?您的工作太辛苦了。Java为此提供了类,如
周日
的枚举,以及表示实体的类,如
年月
。无需自行滚动。有关中代码的类似问题,请参阅一个完整的工作示例。在您的示例中,语句
inth=(q+(13*(m+1)/5)+K+(K/4)+(J/4)+5*J)%7;
h
设置为0。这是有意的吗?在任何情况下,它都会导致
output
不被设置,这就是打印
null
的原因。关键是您使用了(h-1)在下面的代码和0oh..抱歉,请删除多余的“**”在我的代码中大约为+1。正如我只想强调的,不幸的是,它仍然以“null”而不是“SATURDAY”输出。感谢您的帮助!有没有其他可能的方法来解决这个问题?一旦调试,就非常清楚了。您的程序不正确。感谢您的帮助,给出的公式是“Zellar的同余”计算一周中的某一天,我需要在代码中实现该公式。
    //calculating h value
    int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
        output = Day.values()[(h + 6) % 7]; //getting respective  enum value
Enter the date as day month year : 25 5 2019
25/5/2019 is a SATURDAY located in the FOURTH week of MAY 2019


The Calendar of MAY 2019 is :
Su Mo Tu We Th Fr Sa
         1  2  3  4 
5  6  7  8  9  10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31