[英]Java - For-loop
I'm writing a for-loop for an assignment in school. 我正在为学校的作业写一个for循环。 The loop will write the min number, ex. 循环将写入最小编号,例如。 26 and will increase with 7 every turn in the loop until it reaches max, ex. 26,并且将在循环中每转7圈增加一次,直到达到最大值,例如。 112. Between every number, the will also be written a comma - ",". 112.在每个数字之间,还会在逗号之间写上“,”。 But not after the last number. 但不在最后一个数字之后。
Right now my code looks like this: 现在,我的代码如下所示:
int min=26;
int max=112;
for(int i=min; i<=max; i+=7)
{
if(i!=max)
{
System.out.print(i+", ");
}
else
{
System.out.print(i);
}
}
Right now the last number will have a comma... Where's my problem? 现在最后一个数字会有一个逗号...我的问题在哪里?
Others have explained that i
may not ever be equal to max
. 其他人解释说, i
可能永远不会等于max
。 For a case like this, it's easier to wait until the next loop iteration to print the comma: 对于这样的情况,等待下一次循环迭代以打印逗号会更容易:
for(int i=min; i<=max; i+=7)
{
if(i!=min)
{
System.out.print(", "+i);
}
else
{
System.out.print(i);
}
}
(Often I'll set up a separate boolean
named first
to see if I'm going through the first iteration; it's set to true
before the loop and false
at the end of the loop.) (通常,我将设置一个单独的boolean
first
以查看是否要进行第一次迭代;在循环之前将其设置为true
,在循环结束时将其设置为false
。)
PS There are other ways to solve the problem, such as changing the i==max
condition or computing the actual maximum. PS还有其他方法可以解决此问题,例如更改i==max
条件或计算实际最大值。 However, the above approach can be applied in lots of cases that don't have anything to do with stepping by a fixed amount, and where it might not be as easy to figure out ahead of time when the loop will stop. 但是,上述方法可用于许多情况,这些情况与以固定量步进没有任何关系,并且在循环停止之前可能很难事先弄清楚。
It never enters else
loop, because in your code max = 112
, but your for
loop won't reach that value. 它永远不会进入else
循环,因为在您的代码中max = 112
,但是您的for
循环不会达到该值。 (It reaches 110 after increments of 7). (以7为增量达到110 )。 So it will only execute the `if' loop and print the strings with comma, and then exit. 因此,它将仅执行“ if”循环并以逗号打印字符串,然后退出。
Since 'i' will never be equal to 'max', the else clause is not executed. 由于“ i”永远不会等于“ max”,因此不会执行else子句。
If 'min' is always less than 'max', then you can also do: 如果“最小值”始终小于“最大值”,那么您还可以执行以下操作:
int min=26;
int max=112;
System.out.print(min);
for(int i=min+7; i<=max; i+=7)
{
System.out.print("," + i);
}
After 12 loops: 26+7*12 = 110. It will exit the loop and the second condition will never be met. 在12个循环之后:26 + 7 * 12 =110。它将退出循环,并且永远不会满足第二个条件。
int min=26;
int max=112;
for(int i=min; i<=max; i+=7) {
if(i <= max-7)
System.out.print(i+", ");
else
System.out.print(i);
}
Chances are that in the last iteration of the loop, i
is not equal to max
, given that your increment step y 7
. 鉴于您的增量步长y 7
,在循环的最后一次迭代中, i
可能不等于max
。 Actually, your loop runs from 26
to 112
, that's 86
which is not multiple of 7
. 实际上,您的循环从26
到112
,即86
,不是7
倍数。
You should be fine if you calculate your max value based on min and step, something like.- 如果您根据最小和步进来计算最大值,则应该没问题,例如-
int step = 7;
int min = 26;
int max = min * step;
for(int i = min; i <= max; i += step)
Your max value is 112 and your min value is 26. You're incrementing 7 at each iteration. 最大值是112,最小值是26。每次迭代都将增加7。 If you subtract 26 from 112, you will see that the result is not multiple of 7, which means your loop is not stopping when i
is exactly 112. Rather, your loop ends before i
reaches value 112. That's why you see a comma in the end of the printed string. 如果从112中减去26,您将看到结果不是7的倍数,这意味着当i
恰好是112时循环不会停止。相反,循环在i
达到值112之前结束。这就是为什么在其中看到逗号的原因打印字符串的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.