简体   繁体   English

java while循环无法按预期工作

[英]java while loop not working as expected

I am using while loop to run the logic until both the int values are same.below is the code and compiler executes the while condition, it skips. 我正在使用while循环运行逻辑,直到两个int值都相同。下面是代码,编译器执行while条件,它会跳过。

        while(paramCnt == threshold_value){
            ps.setString(paramCnt++, "abc");
        }

code is not working when paramCnt value is less than threshold_value. paramCnt值小于threshold_value时,代码paramCnt I want to run it until both values are equal. 我想运行它,直到两个值相等。

I was not sure of where i am doing wrong. 我不确定自己在哪里做错了。 any help is appreciated. 任何帮助表示赞赏。

A while loop executes the code between it's brackets while a condition is true. 当条件为真时,while循环将在括号之间执行代码。 Your condition is not true from the start. 从一开始,您的情况就不正确。 In order to loop until both values are equal you could change your condition to: 为了循环直到两个值相等,您可以将条件更改为:

while(paramCnt != threshold_value){
    ...
}

Now you would be looping until paramCnt and threshold_value are equal. 现在,您将循环播放,直到paramCnt和threshold_value相等。

If you want to loop until they equal, it means keep looping if they don't equal: 如果要循环直到它们相等,则意味着如果不相等则继续循环:

while(paramCnt != threshold_value){
     ps.setString(paramCnt++, "abc");
}

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

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