gpt4 book ai didi

java - 为什么这个方法总是返回零?

转载 作者:行者123 更新时间:2023-12-01 17:38:22 25 4
gpt4 key购买 nike

我正在尝试创建一个 Date() 类,但是我认为我的变量可能无法正常工作或者我违反了一些晦涩的类规则?我得到的一条线索是在调试过程中......它引导我到“thread.Class”并显示以下代码:

private void exit() {
if (group != null) {
group.remove(this);
group = null;
}

我不确定这意味着什么。这是我第一次创建自己的类(class)。我认为这可能会导致“System.out.println(today.daysTo(yearFromNow));”返回零。也可能和我的一些变量的范围有关。我的教授告诉我们应该使用私有(private)变量,但他并不完全清楚,至少对我来说是这样。这是我到目前为止的代码:

public class Date {

private int year;
private int month;
private int day;

public static void main(String[] args) {
Date today = new Date(2010,10,22);
Date today2 = new Date(2010,10,22);
Date yearFromNow = new Date(2011,10,22);
System.out.println(today.daysTo(yearFromNow));
System.out.println(today.getMonth());
System.out.println(today.getYear());
System.out.println(today.equals(today2));
System.out.println(today.compareTo(yearFromNow));
System.out.println(today.toString());
}

public Date (int year1, int month1, int day1){

if (year1 <= 0 || month1 <= 0 || month1 > 12 || day1 <= 0 || day1 > 31){
throw new IllegalArgumentException("Input a valid d/m/y.");
}
year = year1;
month = month1;
day = day1;
}

//Modifies current date by adding specified number of days; Not completed yet.
public void addDays (int days){
int numberOfDays = daysBeforeMonth(this.month, this.year)+this.day+days;
if (numberOfDays <= 31){
this.month = 1;
}
}

//Returns number of days to specified date.
public int daysTo (Date d){
int presentDate = dayOfYear(this.day, this.month, this.year);
int futureDate = dayOfYear(d.day, d.month, d.year);
return (futureDate - presentDate);
}

//Returns the number of the month in specified Date;
public int getMonth(){
return this.month;
}

//Returns the number of the year in specified Date;
public int getYear (){
return this.year;
}

// Reports whether or not this and d represent the same date.
public boolean equals (Date d){
return this.day == d.day &&
this.month == d.month &&
this.year == d.year;
}

// Returns negative if this is earlier than d, returns positive
// if this is later than d, and returns zero otherwise.
public int compareTo (Date d){
if (this.month < d.month || this.day < d.day || this.year < d.year){
return -1;
}
if (this.month > d.month || this.day > d.day || this.year > d.year){
return 1;
}
else return 0;
}

// Converts this Date into string form, using the format
// mm/dd/yyyy, as in 07/22/2006.
public String toString (){
String stringDate = month+"/"+day+"/"+year;
return stringDate;
}

// takes an int year and tests whether year is divisble by 4, but
// not 100 OR if it's divisible by 400 => is leap year, else not.
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
}

else if (year % 400 == 0) {
return true;
}

else {
return false;
}
}

//Returns how many days before a certain month, else returns 0.
public static int daysBeforeMonth(int month, int year) {
int Feb = 28;
if (isLeapYear(year) == true) {
month = 29;
}
if (month == 1) {
return 0;
} else if (month == 2) {
return 31;
} else if (month == 3) {
return Feb + 31;
} else if (month == 4) {
return Feb + 61;
} else if (month == 5) {
return Feb + 92;
} else if (month == 6) {
return Feb + 122;
} else if (month == 7) {
return Feb + 153;
} else if (month == 8) {
return Feb + 184;
} else if (month == 9) {
return Feb + 215;
} else if (month == 10) {
return Feb + 245;
} else if (month == 11) {
return Feb + 276;
} else if (month == 12) {
return Feb + 306;
}

else {
return 0;
}
}
//Returns how many days have passed in reference to a specific date.
public static int dayOfYear(int day, int month, int year){
return daysBeforeMonth(month, year) + day;
}

}

如果有人可以向我解释一下我的逻辑哪里错了,那就太好了!谢谢!

最佳答案

旁白:

if (isLeapYear(year) == true) {
month = 29;
}

应该是:

if (isLeapYear(year) == true) {
Feb = 29;
}

我认为。

但我认为主要问题是您正在比较两个日期的 dayOfYear。

考虑 1 月 1 日为 dayOfYear:

  • 2010-01-01:年份为 1。
  • 2011-01-01:年份为 1。

所以差值是0。

关于java - 为什么这个方法总是返回零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3998681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com