2016-11-16 179 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