我正在寻找一种方法将值index.totalNumCitations
设置为我在while循环中设置的值。我最初将值设置为1
,因此for循环至少会执行一次。我尝试在for循环之前获取值,但也无法正常工作。如果有人能指出我正确的方向,我将非常感激。
for (int i = 0; i < index.totalNumCitations; i++) {
while (inputInteger > 50 || inputInteger < 0) {
inputInteger = Integer.parseInt(sc.nextLine());
index.Index(inputInteger);
}
这是while循环中使用的方法
public void Index(int totalNumCit) {
if (totalNumCit <= 50 && totalNumCit > 0) {
this.totalNumCitations = totalNumCit;
citationIndex = new Citation[totalNumCit];
} else {
System.out.println("Error: Please enter a number between 0 and 50.");
}
}
答案 0 :(得分:1)
在提问时,请尝试更一般地描述您要做的事情。 人们可能能够提出更好的解决方案。
但是对于这种情况,我建议你在for循环之前放置while循环。因为如果不在for循环中的其他地方更改inputInteger的值,则while循环将仅在for循环的第一个循环中运行。
while (inputInteger > 50 || inputInteger < 0) {
inputInteger = Integer.parseInt(sc.nextLine());
index.Index(inputInteger);
}
for (int i = 0; i < index.totalNumCitations; i++) {
//whatever you want to do
}