Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA初学者:IF语句没有按预期工作-应该在什么时候调用代码体?_Java_Arrays_If Statement_Methods - Fatal编程技术网

JAVA初学者:IF语句没有按预期工作-应该在什么时候调用代码体?

JAVA初学者:IF语句没有按预期工作-应该在什么时候调用代码体?,java,arrays,if-statement,methods,Java,Arrays,If Statement,Methods,几周新的编码!我被指派制作一个方法,用户可以在上午9点到下午5点之间预约。我制作了一个大小为8的布尔数组,名为availability(0=9am,1=10am,2=11am,3=12am…8=17pm)。True表示时间可用,false表示时间不可用 该方法具有描述、小时和持续时间等参数。我的方法是在选定的时间预约 但是,比方说,我在上午9点预约了一个时间为1小时的约会。-->0现在为false。 接下来,我预订了上午11点的预约,预约时间为1小时。-->2现在是假的。 然后我在上午10点预约

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

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

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

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

我尝试的是在代码开始时将布尔变量设置为true,如果if语句发现了false可用性,则将check变量设置为false。这将停止下一个代码块,其中IF语句检查“check”是否为true(如果持续时间内的所有插槽都为true,因为它没有设置为false)

我一直试图在不寻求外界支持的情况下找到问题并加以解决,但现在已经到了这样的地步:盯着电脑屏幕不是一种很好的学习方式,有人能指出一个我可以学习的问题是最好的选择


如果我得到回复,谢谢!:)如果这篇文章有什么不允许的地方,对不起,第一次

您错过了结束括号,而不是整天循环检查它们是否已预订,而是只检查第一个,然后进行预约。它的工作,因为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

您错过了结束括号,而不是整天循环检查它们是否已预订,而是只检查第一个,然后预约。它的工作,因为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语句可能具有的效果)。祝你今天愉快。啊,谢谢你!这真的有助于理解我的逻辑缺陷和我处理问题的方法!(确实需要记住return语句可能具有的效果)。祝你有美好的一天。
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
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;

  //...

}