简体   繁体   中英

what's wrong in this for loop?

int i, k, j;
    for(j=0; j<5; j++)
    for(i=0,k=0; i<5,k<5; i++,k++)
        System.out.print(c[i]+" : "+p[i][j][k]);

I got compiler error for this statement

for(i=0,k=0; i<5,k<5; i++,k++)

what is wrong here?

In java for loop the condition should give be a boolean value so you should use either

for(i=0,k=0; i<5&&k<5; i++,k++)

or

for(i=0,k=0; i<5||k<5; i++,k++)

Use

for(i=0,k=0; i<5&&k<5; i++,k++)

OR

for(i=0,k=0; i<5||k<5; i++,k++)

instead of

for(i=0,k=0; i<5,k<5; i++,k++)

您需要将其更改为以下内容,以使循环具有布尔表达式:

for(i=0,k=0; i<5 && k<5; i++,k++)

If you want to check the both condition then just try the || or && so that it can run....

for(i=0,k=0; i<5||k<5; i++,k++)
for(i=0,k=0; i<5&&k<5; i++,k++)

int i, k, j;
for(j=0; j<5; j++)
    for(i=0,k=0; i<5||k<5; i++,k++)
        System.out.print(c[i]+" : "+p[i][j][k]);

Why on Earth do you loop on two indistiguishable int s i and k ? according your code k is just as synonym of i and can be easily removed. Do it simply as

  for (int j = 0; j < 5; j++)
    for (int i = 0; i < 5; i++)
      System.out.print(c[i] + " : " + p[i][j][i]); // "k" is "i"

As said before, the condition must be a single one.

If you want it to run while at least one of the two is under the given value use:

i<5 || k<5

If you want it to run untill one of the two surpasses the given value use:

i<5 && k<5

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