gpt4 book ai didi

java - 如何调用和打印方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:09 25 4
gpt4 key购买 nike

我有我的主类Date.Java

Date.Java 的作用是:
-设置月份日期和年份,并确保其有效输入
-确定您输入的月份有多少天
-确定自您输入之日起已经过去了多少天
-确定距离您输入的日期还剩多少天
- 确定该年份是否为闰年

我还有一个测试类TestDate.java
-它要求用户输入并将其发送到 Date.java

我的问题是我只能让程序打印用户输入的日期。我将如何调用其他方法并让它们打印它们返回的内容?

日期.java:

public class Date
{
int Day;
int Month;
int Year;
int numberOfDays;
int daysPassed;
int daysRemaining;
int M;
int D;
int Y;



public Date (int Day, int Month, int Year)
{
setDate(Month, Day, Year);

}

public void setDate (int Day, int Month, int Year)
{
setMonth(Month);
setDay(Day);
setYear(Year);
}


//-------------------SETTERS----------------------
public void setMonth(int Month)
{
M = ((Month>0&&Month<13) ?Month:1); //conditional statement that checks to see if the Month is valid
}

public void setDay(int Day)
{
D = ((Day>=1&&Day<=365) ?Day:1); //conditional statement that checks to see if the day is valid

}

public void setYear(int Year)
{
Y = ((Year>=1000&&Year<=9999) ?Year:1900); //conditional statement that checks to see if the Year is valid
}

//-------------------GETTERS----------------------
public int getMonth()
{
return M;

}

public int getDay()
{
return D;

}


public int getYear()
{
return Y;
}

public String toString()
{
return String.format("%d-%d-%d", getMonth(), getDay(), getYear());
}

public static boolean isLeapYear(int getYear)
{

if (getYear%4 == 0)
return true;


else
return false;



}

public int daysOfMonth(int getMonth, int numberOfDays, boolean isLeapYear)
{
if (getMonth==1) //jan
numberOfDays = 31;

//---------------------------------------Feb
if (isLeapYear == true) //feb
{
if (getMonth==2)
numberOfDays = 29;
}
if (isLeapYear == false) //feb
{
if (getMonth==2)
numberOfDays = 28;
}
//---------------------------------------Feb

if (getMonth==3) //march
numberOfDays = 31;
if (getMonth==4) //april
numberOfDays = 30;
if (getMonth==5) //may
numberOfDays = 31;
if (getMonth==6) //june
numberOfDays = 30;
if (getMonth==7) //july
numberOfDays = 31;
if (getMonth==8) //august
numberOfDays = 31;
if (getMonth==9) //sept
numberOfDays = 30;
if (getMonth==10) //oct
numberOfDays = 31;
if (getMonth==11) //nov
numberOfDays = 30;
if (getMonth==12) //dec
numberOfDays = 31;


return numberOfDays;
}

public int daysPassedInYear(int getMonth, int Day, int getDay, int numberOfDays, boolean isLeapYear)
{
if (getMonth==1)
Day = getDay-31;


if (isLeapYear = true)
{
if (getMonth==2)
daysPassed = (numberOfDays-getDay)-60;
if (getMonth==3)
daysPassed = (numberOfDays-getDay)-91;
if (getMonth==4)
daysPassed = (numberOfDays-getDay)-121;
if (getMonth==5)
daysPassed = (numberOfDays-getDay)-152;
if (getMonth==6)
daysPassed = (numberOfDays-getDay)-182;
if (getMonth==7)
daysPassed = (numberOfDays-getDay)-213;
if (getMonth==8)
daysPassed = (numberOfDays-getDay)-244;
if (getMonth==9)
daysPassed = (numberOfDays-getDay)-274;
if (getMonth==10)
daysPassed = (numberOfDays-getDay)-305;
if (getMonth==11)
daysPassed = (numberOfDays-getDay)-335;
if (getMonth==12)
daysPassed = (numberOfDays-getDay)-366;
}




if (isLeapYear = false)
{
if (getMonth==2)
daysPassed = (numberOfDays-getDay)-59;
if (getMonth==3)
daysPassed = (numberOfDays-getDay)-90;
if (getMonth==4)
daysPassed = (numberOfDays-getDay)-120;
if (getMonth==5)
daysPassed = (numberOfDays-getDay)-151;
if (getMonth==6)
daysPassed = (numberOfDays-getDay)-181;
if (getMonth==7)
daysPassed = (numberOfDays-getDay)-212;
if (getMonth==8)
daysPassed = (numberOfDays-getDay)-243;
if (getMonth==9)
daysPassed = (numberOfDays-getDay)-273;
if (getMonth==10)
daysPassed = (numberOfDays-getDay)-304;
if (getMonth==11)
daysPassed = (numberOfDays-getDay)-334;
if (getMonth==12)
daysPassed = (numberOfDays-getDay)-365;
}


return daysPassed;

}



public int daysRemainingInYear(int dayspassed, boolean isLeapYear, int daysRemaining)
{
if (isLeapYear = true)
daysRemaining = (366 - dayspassed);


if (isLeapYear = false)
daysRemaining = (365 - dayspassed);

return daysRemaining;
}






}

TestDate.java:

import javax.swing.JOptionPane;



public class TestDate {
public static void main(String[] args) {


int month =Integer.parseInt(JOptionPane.showInputDialog("What month do you want(in number form ex. Jan = 1?"));
int day =Integer.parseInt(JOptionPane.showInputDialog("What day do you want within the month?"));
int year =Integer.parseInt(JOptionPane.showInputDialog("What year do you want"));


Date setDateObject = new Date(month, day, year);
System.out.println(setDateObject.toString());

}

}

最佳答案

您可以调用 Date 对象上的其他方法并打印它们的返回值。

Date date = new Date(1, 2, 2003);

System.out.println(date.getYear());
System.out.println(date.isLeapYear());

你使用参数的方式很奇怪。 getMonth、numberOfDays、isLeapYear 等参数用于将信息传递到函数中。 getMonth 和 isLeapYear 对于函数正常工作至关重要,但 numberOfDays 不是必需的。您可以像这样声明该函数:

public int daysOfMonth(int getMonth, boolean isLeapYear)
{
if (getMonth==1) //jan
return 31;

//---------------------------------------Feb
if (isLeapYear && getMonth==2)
return 29;
}

if (/* !isLeapYear && */ getMonth==2)
return 29;
}
//---------------------------------------Feb

if (getMonth==3) //march
return 31;
if (getMonth==4) //april
return 30;
if (getMonth==5) //may
return 31;
if (getMonth==6) //june
return 30;
if (getMonth==7) //july
return 31;
if (getMonth==8) //august
return 31;
if (getMonth==9) //sept
return 30;
if (getMonth==10) //oct
return 31;
if (getMonth==11) //nov
return 30;
if (getMonth==12) //dec
return 31;


return -1; // error
}

然后您可以像这样创建一个日期并调用 daysOfMonth 函数。

Date date = (2, 29, 2004);
System.out.println(date);
System.out.println("Is this date a leap year? " + date.isLeapYear());
System.out.println("How many days in this month? " + date.daysOfMonth(date.getMonth(), date.isLeapYear());

关于java - 如何调用和打印方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499811/

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