【问题标题】:breaking out of an "if" statement and going back to the start of a "for" loop跳出“if”语句并回到“for”循环的开头
【发布时间】:2014-05-30 15:10:57
【问题描述】:

因此,对于课堂作业,我必须接受用户输入并分配我的沙鼠对象每天可以使用的“食物”量。在这种情况下,我已经从用户那里获取了每日最大食物量,如果用户尝试输入超过每日最大食物量的值,我需要向用户提供错误消息。

如果是这种情况,则需要重新提示它们输入沙鼠吃的食物量。

我似乎不知道如何跳出“if”语句并回到“for”循环的顶部。这是我的代码:

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    if (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        break;
    } else {
        gerbilConsumption[index] = amountOfFood;
    }
}

【问题讨论】:

  • 查看continue
  • 对不起,但没有什么可以否决这篇文章。人们经常将表现出缺乏努力的问题与表现出努力但可能不知道某些事情的问题混为一谈,这就是首先提出问题的原因。
  • 你为什么要“打破你的 if 语句”,你让事情变得更加混乱。
  • 为什么需要?
  • 是的,这是我的第一个 comp sci 课程,所以我对一切都很陌生。不过感谢您的帮助。

标签: java if-statement for-loop break


【解决方案1】:

如果你想重新启动 for 循环,那么这样做:

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    if (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        j = -1;  //instead of break
    } else {
        gerbilConsumption[index] = amountOfFood;
    }
}

变量j将在下一次迭代中变为0,循环重新开始。

另一方面,如果您只想继续for 循环,请使用continue 代替break。但在这种情况下这是没有意义的,因为这会自动发生。

由于不清楚这里需要什么,如果您想保持相同的迭代级别,我建议您在内部使用 while 循环。

for (int j = 0; j < numberOfFoods; j++) {
    System.out.println(gerbilId[index].substring(index)
            + " eats how many " + foodNames[j] + "s per day");
    int amountOfFood = keyboard.nextInt();

    while (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        amountOfFood = keyboard.nextInt();
    }
    gerbilConsumption[index] = amountOfFood;
}

【讨论】:

  • 你是对的,但我认为他需要 j--; 而不是 j = -1; 因为你想再次执行相同的循环迭代,而不是再次启动整个 for 循环
  • @jimmycarr 目前还不清楚 OP 想要什么,但对您的回复 +1。
  • @carloscollazo 它可以完美运行,直到您在第二个或第三个 numberOfFoods 循环中输入超过最大值的数量,它会意外地跳回 0。
  • @CodeCamper 我认为这就是 OP 想要的。原因,我不知道。
  • 正如有些人所说,j-- 实际上更适合我正在尝试做的事情。
【解决方案2】:

如果用户尝试输入值,则向用户提供错误消息 高于每日最大值。

如果是这种情况,他们需要重新提示输入金额 沙鼠吃的食物。

你不能跳过你的 for 循环,因为你正在添加到 J 中,这会让你感到困惑。

如果您需要完成上述操作,请使用这样的代码(将其插入您的 for 循环)

while(true){
//prompt amount of gerbil food
//if input equal or below daily max then BREAK;
//tell user they are above daily max
}

或者使用您帖子中的代码...

int amountOfFood;
while(true){
 amountOfFood=keyboard.nextInt();
 if (amountOfFood <= foodMax[j]) break;
 System.out.println("Error. Please input a valid amount of food");
}
 gerbilConsumption[index] = amountOfFood;

如您所见,我倒转了您的支票。最好检查什么是有效的,而不是检查什么是无效的。

【讨论】:

    【解决方案3】:

    使用continue 的答案是错误的,因为它们不是您在这种情况下要寻找的。 continue 会将您转到下一种食物类型,但如果输入无效,您希望留在同一种食物上。您需要执行j--; 才能再次使用该号码

    if (amountOfFood > foodMax[j]) {
        System.out.println("Error. Please input a valid amount of food");
        j--;
    } else {
        gerbilConsumption[index] = amountOfFood;
    }
    

    【讨论】:

    • 我假设您对我的回答投了反对票。我已经更新了,请看看我的推理并考虑删除你的反对票。
    • 不,这根本不是我,我很遗憾看到你因为写了一些仍然有用的东西而被否决,即使在这种情况下不是他正在寻找的东西。
    【解决方案4】:

    您还可以重新构造以使用 do...while 循环 -

    for (int j = 0; j < numberOfFoods; j++) {
        System.out.println(gerbilId[index].substring(index)
                + " eats how many " + foodNames[j] + "s per day");
        int amountOfFood=-1;
    
        do 
        {
           amountOfFood = keyboard.nextInt();
           if (amountOfFood > foodMax[j] || amountOfFood <0) {
            System.out.println("Error. Please input a valid amount of food");
            amountOfFood=-1;
           }
        } while (amountOfFood == -1);
    
        gerbilConsumption[index] = amountOfFood; 
    }
    

    【讨论】:

      【解决方案5】:

      技术上,您正在寻找 Java 关键字 continue

      关键字break 将完全跳出for循环。

      关键字 continue 将跳过 for 循环的其余迭代并开始它的下一次迭代。

      例如:

      //loops 3 times
      for(int i = 0; i < 3; i++) {
          if(i == 1) {
              continue;
          }
          System.out.println("Iteration #" + i);
      }
      

      将打印:

      Iteration #0
      Iteration #2
      

      这回答了你的问题。

      但是,正如其他人指出的那样,continue 实际上并不是您想要的特定示例。你真正想要使用的只是j--。但是为了将来参考,您现在知道如何跳出“if”语句并返回到 for 循环的顶部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-02
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多