因此,对于课堂作业,我必须接受用户输入并指定我的沙鼠对象每天可以使用的"食物"量.在这种情况下,我已经每天从用户那里获取最大量的食物,并且如果他们试图输入高于每日最大值的值,则需要向用户发出错误消息.
如果是这种情况,则需要重新提示他们输入沙鼠吃的食物量.
我似乎无法弄清楚如何打破"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;
}
}
Run Code Online (Sandbox Code Playgroud)