java for 循环不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4574020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:01:41  来源:igfitidea点击:

java for loop not working

javasyntaxfor-loopiteration

提问by Steve

I hope this isn't a stupid question but I have looked up every example I can find and it still seems like I have this code right and it still isn't working... I enter one number and it moves on to the next line of code instead of looping. I'm using this to fill an array with user input numbers. I appreciate any help, thanks.

我希望这不是一个愚蠢的问题,但我已经查找了我能找到的每个示例,但似乎我的代码是正确的,但它仍然无法正常工作......我输入一个数字,然后它移到下一个代码行而不是循环。我正在使用它来填充用户输入数字的数组。我感谢任何帮助,谢谢。

for(i=0; i<9; i++);
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();  
    Sum += Num[i];      
    Product *= Num[i];      
}   

回答by Bert F

The ;at the end of the for loop is taken as an empty statement, the equivalent of an empty block for your for-loop. The compiler is reading your code as:

;在该月底循环被取空语句,相当于一个空块,为您的for循环。编译器正在阅读您的代码:

int i;
....
for(i=0; i<9; i++)
    /* no-op */;

/* inline block with no relation to for-loop */
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();  
    Sum += Num[i];      
    Product *= Num[i];      
} 

Remove the ;to get your intended behavior.

删除;以获得您的预期行为。



If you don't need the ioutside of the loop, you could move its declaration within the forstatement.

如果您不需要i循环的外部,您可以在for语句中移动它的声明。

for(int i=0; i<9; i++)
{
   // `i` is only usable here now
}
// `i` is now out of scope and not usable

Using this syntax when the erroneous semicolon ;was present would have produced a compile error that would alerted you to the erroneous ;earlier. THe compiler would see this:

当存在错误的分号时使用此语法;会产生编译错误,该错误会提醒您;之前的错误。编译器会看到:

for(int i=0; i<9; i++)
    /* no-op */;

/* inline block with no relation to for-loop */
{  
    System.out.println ("Please enter a number:");  
    Num[i] = keyboard.nextDouble();     // compile error now - `i` is out-of-scope
    Sum += Num[i];      
    Product *= Num[i];      
} 

This would be an example why it is good practice to limit the scope of variables when possible.

这将是一个示例,说明为什么在可能的情况下限制变量的范围是一种很好的做法。

回答by kellogs

Noticed the ';' at the end of for(i=0; i<9; i++); ? ^_^

注意到';' 在 for(i=0; i<9; i++); ? ^_^

回答by Sagar Varpe

remove the last character semicolon from for loop line ............

从 for 循环行中删除最后一个字符分号 .....................

回答by Roland Illig

To avoid this mistake in future you should always use a fresh variable in a for loop. Instead of:

为避免将来出现此错误,您应该始终在 for 循环中使用新变量。代替:

for (i = 0; ...

write

for (int i = 0; ...

That way it would be a compile-time error, since the variable iwould not be in scope in the following block.

这样就会出现编译时错误,因为变量i将不在以下块的范围内。

回答by Jim Rush

There shouldn't be a semicolon at the end of the first line. It indicates your loop is empty.

第一行的末尾不应该有分号。它表明您的循环是空的。

回答by Inca

The answer has already been given, but I'd like to add that if you are using an IDE*, there will probably be a warning for those kinds of empty statements and other easy to make, easy to overlook type of mistakes (assinging instead of comparing in conditions for example).

答案已经给出,但我想补充一点,如果您使用的是 IDE*,那么可能会针对那些空语句和其他容易做出、容易忽视的错误类型发出警告(改为 assing例如在条件中进行比较)。

回答by Adil Mehmood

just to let you know. something like this:

只是让你知道。像这样:

for(;;)
  ;

should send your program into busy-waiting. happened to me in the early days. :)

应该将您的程序发送到忙等待状态。早期发生在我身上。:)

回答by rishabh jain

Just remove that semi-colon after the "for(....)"

只需删除“for(....)”之后的分号

for(i=0; i<9; i++); //<---------- over here

you don't have to put a semi-colon over here. Most probably you were aware of that, it happens. Additional points: The syntax for "for" is:

你不必在这里放一个分号。很可能你已经意识到这一点,它发生了。附加点:“for”的语法是:

for(initializer; condition; change){
//code here
}

The syntax for "while" is:

“while”的语法是:

while(condition without semi-colon){
//code here
}

The syntax for "do-while" is:

“do-while”的语法是:

do{
//code here
}while(condition);//<---------------semi-colon here.

Most probably you were confused between "for" and "do-while" loops, that happens. I too, when I was a beginner, used to be confused of such things.

您很可能在“for”和“do-while”循环之间感到困惑,这种情况发生了。我也是,当我是初学者时,曾经对这样的事情感到困惑。