简体   繁体   中英

Android Studio Java: How to make a button break a while loop

I have question how to make button to break while loop. I don't have idea that this method exist at all.

For example i have while loop:

while(x==10000){
  x++;
}

void onClick(view v){

    // break the while loop 
}

And i want to make a button, that when i clicked it i break the while loop and get x value. eg x=9834. Ofc i want this button to most advanced function. I appreciate if you paste me code, how to make it. Thanks a lot for help!

Simply add another condition to break the while loop, that you control from your button :

boolean keepGoing = true;
while ((x==10000) && (keepGoing)) {
  x++;
}

void onClick(view v){
    keepGoing = false;
}

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