Java 设置具有参数的值
我正在寻找一种方法,将index.totalNumCitations的值设置为我在while循环中设置的值。我最初将该值设置为Java 设置具有参数的值,java,while-loop,Java,While Loop,我正在寻找一种方法,将index.totalNumCitations的值设置为我在while循环中设置的值。我最初将该值设置为1,以便for循环至少执行一次。我尝试在for循环之前获取值,但这也不起作用。如果有人能给我指出正确的方向,我将非常感激 for (int i = 0; i < index.totalNumCitations; i++) { while (inputInteger > 50 || inputInteger < 0) {
1
,以便for循环至少执行一次。我尝试在for循环之前获取值,但这也不起作用。如果有人能给我指出正确的方向,我将非常感激
for (int i = 0; i < index.totalNumCitations; i++) {
while (inputInteger > 50 || inputInteger < 0) {
inputInteger = Integer.parseInt(sc.nextLine());
index.Index(inputInteger);
}
for(int i=0;i50 | |输入整合器<0){
inputInteger=Integer.parseInt(sc.nextLine());
索引。索引(输入整合器);
}
这是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.");
}
}
public void Index(int totalNumCit){
if(totalNumCit 0){
this.totalNumCitations=totalNumCit;
引文索引=新引文[totalNumCit];
}否则{
System.out.println(“错误:请输入一个介于0和50之间的数字”);
}
}
提问时,试着更笼统地描述一下你想做什么。
人们可能会想出更好的解决办法
但是对于这种情况,我建议您将while循环放在for循环之前,因为如果您不在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
}
while(输入整数>50 | |输入整数<0){
inputInteger=Integer.parseInt(sc.nextLine());
索引。索引(输入整合器);
}
对于(int i=0;i
索引变量是什么类型的?你到底在问什么?它是一个类。我不确定你指的是否是类。我想知道为什么我不能使用while循环中指定的值。你想要一个带有动态结束边界的for循环
???