for循环计数术语java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19394393/
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
for loop count number of terms java
提问by Bob
Hey guys i'm stuck on count for a for loop... It should be easy, but for some reason i'm over thinking it or something! Any help would be awesome!
嘿伙计们,我一直在为 for 循环计数......这应该很容易,但出于某种原因,我想得太多了!任何帮助都是极好的!
i'm trying to get the total number of terms that passed through %2 and print them out, but when i try it my way it does 11223344556677889910 which i thought was weird! all i want to do is get to say (10).
我正在尝试获取通过 %2 的术语总数并将其打印出来,但是当我按照自己的方式尝试时,它确实是 11223344556677889910,我认为这很奇怪!我只想说(10)。
int counter = 0
for (int i = 1; i < 20; i++) {
if (i % 2 == 1) {
counter += 1;
}
System.out.print(counter);
}
output is 112233445566778899
输出为 112233445566778899
can't use arrays or lists :\
不能使用数组或列表:\
采纳答案by harsh
if
condition seems incorrect, try with: (EDIT: start counter
with 1 and move SOP out of for
loop)
if
条件似乎不正确,请尝试:(编辑:从counter
1开始并将 SOP 移出for
循环)
int counter = 1
for(int i=1;i<20;i++) {
if (i%2==0) //Match for 2
{
counter+=1;
}
}
System.out.print(counter);
回答by Prabhakaran Ramaswamy
You need to print the counter outside of for loop System.out.print(counter);
and looks like i%2==1 is not correct. You need to change to i%2==0
您需要在 for 循环之外打印计数器System.out.print(counter);
,看起来 i%2==1 不正确。你需要改成 i%2==0
int counter = 0
for(int i=1;i<20;i++) {
if (i%2==0)
{
counter+=1;
}
}
System.out.print(counter);
回答by ffxtian
System.out.print is inside your for loop, it's printing each iteration.
System.out.print 在你的 for 循环中,它打印每次迭代。
If you want 10, move the call to .print() outside the for loop, and start your counter at 1 instead of 0.
如果你想要 10,把对 .print() 的调用移到 for 循环之外,并从 1 而不是 0 开始你的计数器。
回答by Theocharis K.
The output is correct in this case. You are printing the total amount of counter
every time the loop is executed. So for i=0 that would be counter=1, for i=1 that would be still counter=1, for i=2 the counter increases to 2, for i=3 the counter remains 2 which is printed etc.
在这种情况下,输出是正确的。您正在打印counter
每次执行循环时的总量。因此,对于 i=0 将是 counter=1,对于 i=1 仍然是 counter=1,对于 i=2 计数器增加到 2,对于 i=3 计数器仍然是 2 被打印等等。
The code is working correctly but there is a fault in it. Move the System.out.print(counter);
outside of the for
loop and it should work correctly. Also remember to initialize your counter
to 1.
代码工作正常,但有错误。移动循环的System.out.print(counter);
外部,for
它应该可以正常工作。还记得将您的初始化counter
为 1。
回答by Michael Yaworski
Take the print
line out of the for loop
, or else it will print the count every iteration (obviously), which is 19 times.
从 中取出print
一行for loop
,否则它将在每次迭代时打印计数(显然),即 19 次。
int counter = 0;
for(int i = 1; i < 20; i++)
{
if (i % 2 == 1) counter++;
}
System.out.print(counter);
回答by Nalaka
You have put System.out.print(counter);
inside the for loop. That's the reason. This will work:
您已放入System.out.print(counter);
for 循环。这就是原因。这将起作用:
int counter = 0;
for(int i=1;i<20;i++) {
if (i%2==1)
{
counter+=1;
}
}
System.out.print(counter);
回答by ArnOld JeAn-GustAve ZOundi
System.out.print(counter);
You should put this line of code outside the for loop to avoid printing out each value of counter
.
您应该将这行代码放在 for 循环之外,以避免打印出counter
.