打破“if”语句并返回“for”循环的开头

时间:2014-04-16 02:00:21

标签: java if-statement for-loop break

因此,对于课程作业,我必须接受用户输入并指定我的沙鼠对象每天可以使用的“食物”量。在这种情况下,我已经每天从用户那里获取最大量的食物,并且如果他们试图输入高于每日最大值的值,则需要向用户发出错误消息。

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

我似乎无法弄清楚如何突破“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;
    }
}

5 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

  

如果用户尝试输入值,则会向用户发送错误消息   高于每日最高

     

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

你不能跳过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 :(得分:0)

您还可以重新构造以使用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; 
}

答案 4 :(得分:0)

从技术上讲,您正在寻找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--。但是为了将来的参考,你现在知道如何打破&#34; if&#34;声明并返回到for循环的顶部。