简体   繁体   中英

I want to loop over an int comparing it with an int array java

I want to compare an int x value with an int array data[i][j] using a do loop. data got the values 1236, 1238, 1240 and 1250. and x = 1237

my loop looks like this:

boolean go = true;

do {
    if (x == data[i][j]) {
        System.out.println("" + x)
        go = false;
    } else {
        x++
    }

} 
while (go)

x is int, go is boolean. Compiler error. Try this.

  do{
       if(x==data[i][j]){
       System.outln(""+x)
       go = false;
    }
    else{x++}
    }

    while(go == true);

I am not sure whether the original asker actually wanted to do this:

for(int i=0;i<100;i++) {
    for(int j=0;j<100;j++) {
        if(x == data[i][j]) {
            System.out.println("found " + x + " at " + i + " " + j);
            break;
        }
    }
}

As in finding the position of x in data.

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