【问题标题】:JAVA BEGINNER: IF statement not working as intended - body of code not being called when it should?JAVA BEGINNER: IF 语句没有按预期工作 - 代码主体没有被调用?
【发布时间】:2020-02-19 02:07:26
【问题描述】:

几周新的编码!我被分配了一种方法,用户可以在上午 9 点到下午 5 点之间进行“预约”。我制作了一个大小为 8 的布尔数组,名为可用性(0 = 9am, 1 = 10am, 2 = 11am, 3 = 12am ... 8 = 17pm。)True 表示时间可用,false 表示时间不可用。

该方法具有描述、小时和持续时间的参数。我的方法是能够在选定的时间预约。

但是,假设我在上午 9 点预订了一个持续时间为 1 的约会。--> 现在 0 为假。 接下来,我在上午 11 点预约了一个持续时间为 1 的约会。--> 2 现在是假的。 然后我在上午 10 点预约,持续时间为 2。--> 1(上午 10 点)应该保持不变,因为如果下一小时(上午 11 点)已经预订,用户不应该在 2 小时内预订,对吗?

我试图在我的代码中实现这一点,但无济于事。我的代码仍然在上午 10 点预订,并将插槽设置​​为 false!从我收集到的信息来看,我认为问题出在第 96 行,if 语句检查持续时间内是否有任何插槽为假。

我尝试的是在代码开头将布尔变量设置为 true,如果 if 语句发现错误的可用性,请将检查变量设置为 false。这将停止下一个代码块,其中一个 IF 语句检查“检查”是否为真(如果在此期间的所有插槽都为真,因为它没有设置为假)。

public boolean makeAppointment(String desc, int duration, int hour) {
  boolean check = true;
  if (!((hour + (duration - 1) > 17))) { // it'll be 10
    int booleanHour = hour - 9; // it'll be 1

    if (booleanHour <= availability.length && booleanHour >= 0 && duration > 0 && duration <= 4) { // This 4 is a placeholder, subject to change.  


      for (int i = booleanHour; i < booleanHour + duration; i++) { 
        if (availability[i] == false) { // This isn't working? Confusion. 
          System.out.println( // response to if false
            "This has hour been booked, please choose another time. We open at 9am, and close at 5pm.");
          System.out.println(check);
          check = false;
          return false;
        } 
        if (check) {
          for (int b = booleanHour; b < booleanHour + duration; b++) {
            availability[b] = false;
            Appointment appoint = new Appointment(desc, duration, b + 9);
            schedule.put(b + 9, appoint);
          }
          return true;
        }

      }
    } else {
      System.out.println("The max duration of a         meeting is 4 hours.");
    }
  }
  return false;
}

这也是我的测试代码。

Day day5 = new Day();
    day5.makeAppointment("hello", 1, 9);
    assertEquals(day5.getAvailability(0), false);

    day5.makeAppointment("hello", 1, 11);
    assertEquals(day5.getAvailability(2), false);

    day5.makeAppointment("hello", 2, 10);
    assertEquals(day5.getAvailability(1), true);

我一直试图在不寻求外部支持的情况下找到问题并解决它,但现在盯着电脑屏幕并不是一种好的学习方式,并且有人可以指出一个问题我可以借鉴是最好的选择!

如果我得到回复,谢谢! :) 如果这篇文章的任何内容是不允许的,对不起,第一次!

【问题讨论】:

    标签: java arrays if-statement methods


    【解决方案1】:

    您错过了右括号,而不是整天循环检查他们是否被预订,而是检查只检查第一个,然后进行预约。它之所以有效,是因为 10 显然是免费的 - 即使您愿意,该程序也不会检查 11

    for (int i = booleanHour; i < booleanHour + duration; i++) { 
        if (availability[i] == false) {
          System.out.println("This has hour been booked, please choose another time. We open at 9am, and close at 5pm.");
          System.out.println(check);
          check = false;
          return false;
        } // this is the end of IF not FOR - only first element was checked!
        if (check) { // yup it's true because 10 is free...
          for (int b = booleanHour; b < booleanHour + duration; b++) {
            availability[b] = false; // here we are making all hours booked no matter they are booked or not already
            Appointment appoint = new Appointment(desc, duration, b + 9);
            schedule.put(b + 9, appoint);
          }
          return true;
        }
      } // and finally this is the end of FOR but it's too late
    

    还请注意,如果您使用 return false,则您将不再访问 return 关键字之后的代码 - 您的 check 条件没有意义


    固定代码可能如下所示:

    public boolean makeAppointment(String desc, int duration, int hour) {
    
      //...
    
      for (int i = booleanHour; i < booleanHour + duration; i++) { 
        if (!availability[i]) {
          System.out.println("This has hour been booked, please choose another time. We open at 9am, and close at 5pm.");
          return false;
        }
      } 
    
      for (int b = booleanHour; b < booleanHour + duration; b++) {
          availability[b] = false; // here we are making all hours booked no matter they are booked or not already
          Appointment appoint = new Appointment(desc, duration, b + 9);
          schedule.put(b + 9, appoint);
      }
    
      return true;
    
      //...
    
    }
    

    【讨论】:

    • 啊,谢谢!这对于理解我的逻辑缺陷和解决问题的方法非常有帮助! (确实需要记住return语句可以有的效果)。祝你有美好的一天。
    猜你喜欢
    • 2022-11-26
    • 2011-11-29
    • 1970-01-01
    • 2017-06-18
    • 2020-07-26
    • 2022-11-20
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多