我正在尝试编写一个能够生成时间表的代码。函数编译得很好,但结果并不是我所期待的。
方法:
public String generateTimeTable(){
//2 arrays for all the months of the year and days of the week.
String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
String[] days = {"Mon","Tue","Wed","Thur","Fri","Sat","Sun"};
DateTime today = new DateTime();
DateTime start = today.plusDays(1).withTime(8, 0, 0, 0); // this makes it start at 8am the next day
String finalString = "On" + " " + days[start.getDayOfWeek()-1] + " "+ start.getDayOfMonth() + " " + months[start.getMonthOfYear() - 1] + "\n"; //This is the string for the first day.
// It should return something like "On Mon 13 February
ArrayList<Course> list = listOfCourses(); //get a sorted list of courses
while (start.toLocalDate().isBefore(this.startOfExams)) { // loop for everyday up until the start of exams
String sub1 = list.get(0).toString() + "...from " + new Interval(start, start.plusHours(3)).toString().substring(11, 16) + " to " + new Interval(start, start.plusHours(3)).toString().substring(41, 46) + "\n";
finalString += sub1;
finalString += "\n";
String break1 = "break...from " + new Interval(start.plusHours(3), start.plusHours(4)).toString().substring(11, 16) + " to " + new Interval(start.plusHours(3), start.plusHours(4)).toString().substring(41, 46) + "\n";
// String break1 = "break..." + new Interval(start.plusHours(3), start.plusHours(4)) + "\n";
finalString += break1;
finalString += "\n";
String sub2 = list.get(1).toString() + "..." + new Interval(start.plusHours(4), start.plusHours(7)) + "\n";
finalString += sub2;
finalString += "\n";
String break2 = "LunchBreak...You need to eat! " + new Interval(start.plusHours(7), start.plusHours(8)) + "\n";
finalString += break2;
finalString += "\n";
String sub3 = list.get(2).toString() + "..." + new Interval(start.plusHours(8), start.plusHours(10)) + "\n";
finalString += sub3;
finalString += "\n";
String break3 = "break..." + new Interval(start.plusHours(10), start.plusHours(11)) + "\n";
finalString += break3;
finalString += "\n";
String sub4 = list.get(3).toString() + "..." + new Interval(start.plusHours(11), start.plusHours(13)) + "\n";
finalString += sub4;
finalString += "\n";
String break4 = "break..." + new Interval(start.plusHours(13), start.plusHours(14)) + "\n";
finalString += break4;
finalString += "\n";
String sub5 = list.get(4).toString() + "..." + new Interval(start.plusHours(14), start.plusHours(15)) + "\n";
finalString += sub5;
finalString += "\n";
String sub6 = list.get(5).toString() + "..." + new Interval(start.plusHours(15), start.plusHours((int) 15.5)) + "\n";
//finalString += sub6;
//finalString += "\n";
start = start.plusDays(1); // Move along to the next day.
finalString += "\n";
finalString += "On" + " " + days[start.getDayOfWeek()-1] + " "+ start.getDayOfMonth() + " " + months[start.getMonthOfYear() - 1] + "\n";
}
return finalString + "EXAMM DAYYY!";
}
然而,在获取此数据后,我尝试使用示例信息进行测试:
public static void main(String[] args) {
// TODO Auto-generated method stub
LocalDate examStart = new LocalDate(2016, 3, 1);
Course chem = new Course("Chemistry", 2, examStart);
Course math = new Course("Mathematics", 5, examStart);
Course phys = new Course("physics", 8, examStart);
Course french = new Course("French", 5, examStart);
Course Bio = new Course("Biology", 3, examStart);
Course eng = new Course("English", 6, examStart);
TimeTable tt = new TimeTable(2016, 3, 1);
tt.addCourse(chem);
tt.addCourse(math);
tt.addCourse(phys);
tt.addCourse(french);
tt.addCourse(Bio);
tt.addCourse(eng);
//FIRST THINGS FIRST: complete testing of updating confidence.
//tt.updateConfidence("French", 1);
String finals = tt.generateTimeTable();
System.out.print(finals);
}
但不是打印我的时间表,而是打印出4月1日星期五 EXAMM DAYYY!
任何人都明白为什么会这样?
答案 0 :(得分:0)
在Java Date
类中,月份从0开始,而不是1。因此,您要制定4月1日的时间表,而不是3月1日。这可能是你的意图;我不确定。
在任何情况下,您都要制定4月1日的时间表,然后将开始日期设置为4月1日加一天,即4月2日。这导致while
循环出现问题,该循环从不运行:开始日期在考试日期之后。
您需要解决的其他问题是您没有遍历您生成的list
。您正在使用固定索引来检索元素,这可能适用于您当前的设置,但它非常脆弱:如果您更改课程数量,则必须记住更改while
循环体。最好使用for
循环遍历列表并打印元素。不过,这是一个你当时没有真正打过的问题,因为你的while
循环根本没有被执行。
这将有助于您学习使用IDE中的调试工具:您可以逐行逐步执行代码,并查看执行的内容。
答案 1 :(得分:0)
似乎你在3月31日运行该程序,所以 程序中使用的日期变量如下所示 examStart = 2016年3月1日和 开始= 3月31日(今天)+1天,即4月1日。
while循环条件被评估为false,即start不在examstart之前 因此,只打印那些在循环外评估的字符串。