2016-11-16 115 views
0

我想製作一個程序,從用戶輸入中獲取一天,然後告訴他們前一天和後一天。用戶還應該能夠輸入要添加的日期和程序應該在當天輸出。星期幾

例如用戶輸入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); 
    } 
} 
+2

如果'theWeekDay'> 7,你希望發生什麼? –

+0

當有人輸入12時,你的函數應該如何知道它應該從哪一天開始? –

回答

0

如果你想承擔更大的價值那7有效,你絕對應該用模運算。這樣的事情:

if(theWeekDay > 7) { 
    theWeekDay = theWeekDay % 7; 
} 

否則,你應該拋出和異常。

+0

我有同樣的想法,但我把它放在AddedDays();我只是把它放在TheDay();在我之後:if(theWeekDay == 0){}並且它工作 – OneU

0

您的if else必須涵蓋所有情況....

後添加其他

else if(theWeekDay == 7){ 
     System.out.println("Sunday"); 
    } 

類似:

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"); 
}else{ 
    System.out.println("Invalid input"); 
} 
return theWeekDay; 
0

正如user629735所說,在您的公式中使用模數。

public int AddedDays(int AddedDays) { 
    getTheWeekDay(); 
    theWeekDay = (theWeekDay + AddedDays) % 7; 
    return TheDay(theWeekDay); 
} 
+0

所以基本上這從TheDay()中移除了條件;並保持它在AddedDays();我試過你的新代碼,顯然它的工作原理,但你會說這是更好或只是另一種選擇? – OneU

+0

你是對的,條件已經在TheDay()中設置。但是把條件放在TheDay()之外是不是更好,這樣當用戶在程序開頭放置0時,這不會給他顯示「星期天」? – Davezedave

+0

哇,你是個天才!甚至沒有注意到 – OneU