简体   繁体   English

Android Studio Java:如何使按钮中断while循环

[英]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循环:

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. 我想做一个按钮, 当我单击它时,我打破了while循环并获得x值。 eg x=9834. 例如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 : 只需添加另一个条件即可打破while循环,您可以通过按钮进行控制:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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