我想製作一個程序,從用戶輸入中獲取一天,然後告訴他們前一天和後一天。用戶還應該能夠輸入要添加的日期和程序應該在當天輸出。星期幾
例如用戶輸入1 =星期一,明天是= 2星期二昨天= 3週日
如果用戶其星期一(1),並增加了12天輸出應爲星期六(6)
的說問題是無論何時「theWeekDay」大於7,它都不會輸出,因爲TheDay();沒有超過7的條件。請幫助我!
非常感謝你!
import java.util.Scanner;
import java.util.Scanner;
public class Problem_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int theWeekDay;
System.out.println("What Day Is It?");
theWeekDay = input.nextInt();
Days one = new Days(theWeekDay);
System.out.println("Today It Is: ");
one.TheDay(theWeekDay);
System.out.println("Yesterday It Was: ");
one.PreviousDay(theWeekDay);
System.out.println("Tomorrow It Is: ");
one.NextDay(theWeekDay);
System.out.println("How Many Days To Add?");
int x = input.nextInt();
System.out.println("Now It Is: ");
one.AddedDays(x);
}
}
class Days {
private int theWeekDay;
public Days(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int getTheWeekDay() {
return theWeekDay;
}
public void setTheWeekDay(int theWeekDay) {
this.theWeekDay = theWeekDay;
}
public int TheDay(int theWeekDay) {
// an arra days of week + then add days in it
if (theWeekDay == 0) {
theWeekDay = theWeekDay + 7;
}
if (theWeekDay == 1) {
System.out.println("Monday");
} else if (theWeekDay == 2) {
System.out.println("Tuesday");
} else if (theWeekDay == 3) {
System.out.println("Wednsday");
} else if (theWeekDay == 4) {
System.out.println("Thursday");
} else if (theWeekDay == 5) {
System.out.println("Friday");
} else if (theWeekDay == 6) {
System.out.println("Saturday");
} else if (theWeekDay == 7) {
System.out.println("Sunday");
}
return theWeekDay;
}
public int PreviousDay(int theWeekDay) {
theWeekDay = theWeekDay - 1;
return TheDay(theWeekDay);
}
public int NextDay(int theWeekDay) {
theWeekDay = theWeekDay + 1;
if (theWeekDay > 7) {
theWeekDay = 1;
}
return TheDay(theWeekDay);
}
public int AddedDays(int AddedDays) {
getTheWeekDay();
theWeekDay = theWeekDay + AddedDays;
return TheDay(theWeekDay);
}
}
如果'theWeekDay'> 7,你希望發生什麼? –
當有人輸入12時,你的函數應該如何知道它應該從哪一天開始? –