简体   繁体   中英

Java - For-loop

I'm writing a for-loop for an assignment in school. The loop will write the min number, ex. 26 and will increase with 7 every turn in the loop until it reaches max, ex. 112. Between every number, the will also be written a comma - ",". 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 . 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.)

PS There are other ways to solve the problem, such as changing the i==max condition or computing the actual maximum. 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. (It reaches 110 after increments of 7). So it will only execute the `if' loop and print the strings with comma, and then exit.

Since 'i' will never be equal to 'max', the else clause is not executed.
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.

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 . Actually, your loop runs from 26 to 112 , that's 86 which is not multiple of 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM