java - How to use a value outside for loop -


in following code need value of vararray[i] executing if-else statements if-else statements executed once. if place if-else statements outside loop if-else statements execute correctly. when place if-else statement inside loop if-else statements executed multiple times.

for (int i=0;i<vararray.length;i++)  {     vararray[i]= rand.nextint(1999)-1000;     system.out.println(vararray[i]);       if(d==vararray[i])     {         system.out.println(d);               system.out.println(i+1);     }       else if(d!=vararray[i])     {          system.out.println(d);         system.out.println(0);     } } 

need on this. have been searching hours

you can use break; exit loop when if statement true.

for (int = 0; < vararray.length; i++) {     vararray[i] = rand.nextint(1999) - 1000;     system.out.println(vararray[i]);      if (d == vararray[i]) {         system.out.println(d);               system.out.println(i + 1);         break;     }     else if(d != vararray[i])     {          system.out.println(d);         system.out.println(0);         break;     } } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -