我有我的主類Date.Java
如何調用和打印方法
Date.Java
做:
-Sets月份日期和年份,並確保其有效輸入
- 確定有多少天是在你輸入一個月
- 確定,因爲這一天有多少天過去了,你進入
- 確定有多少天是從一天離開你進入
- 確定,如果該年是閏年
我也有一個測試類TestDate.java
- 它會要求輸入用戶並將它們發送到Date.java
我的問題是,我只能讓程序打印用戶輸入的日期。我將如何去調用其他方法並讓他們打印他們返回的內容?
Date.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());
}
}
是否有任何其他方式可以打印來自「public int daysOfMonth」或「public int daysPassedInYear」等其他方法的回報? – Hammy23 2014-10-22 03:56:57
是的,看我的編輯。 – DucRP 2014-10-22 04:13:44
真棒!非常感謝!我完全意識到我現在做錯了什麼。 – Hammy23 2014-10-22 04:19:13