首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TimeTable函数不工作

TimeTable函数不工作
EN

Stack Overflow用户
提问于 2016-03-31 10:22:40
回答 2查看 70关注 0票数 0

我正在写一段代码来生成时间表。这个函数编译得很好,但是结果与我所期望的不一样。

方法:

代码语言:javascript
复制
         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!";


}

然而,在获取这些数据之后,我尝试用示例信息对其进行测试:

代码语言:javascript
复制
                  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!

有人知道为什么会这样吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-31 10:35:05

月份是从Java Date类中的0 (而不是1 )索引的。所以你在为4月1日,而不是3月1日制定时间表。这可能是你想要的,我不确定。

在任何情况下,您都要为4月1日构建一个时间表,然后将开始日期设置为4月1日加一天,即4月2日。这会导致while循环出现问题,它永远不会运行:开始日期是考试日期之后。

您需要修复的另一个问题是,您没有遍历生成的list。您正在使用固定的索引来检索元素,这些元素可能适用于当前的设置,但它非常脆弱:如果您更改了课程的数量,您必须记住更改while循环体。最好使用for循环来遍历列表并打印元素。不过,这个问题目前还没有真正解决,因为您的while循环根本没有被执行。

它将帮助您学习如何使用IDE中的调试工具:您可以逐行遍历代码,并查看执行了什么。

票数 0
EN

Stack Overflow用户

发布于 2016-03-31 11:11:14

似乎您在3月31日运行该程序,因此程序中使用的日期变量计算如下: examStart =2016年3月1日,start =31(今天)+1天,即4月1日。

当循环条件被评估为false时,即start不是在examstart之前,因此只有那些字符串被打印出来,而这些字符串是在外部循环中计算的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36330676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档