简体   繁体   中英

How to try a true or false syntax in java

I have the fallowing code

while (b == true){
      System.out.print("input the account balance without tax: ");
      int inner = input.nextInt();
      List[x]=inner;
      x=x+1;
}

and I want to check if my user inputs a word it would do:

b =false

So I would exit this loop. In python we had try() and except. I don't really know what to do in java.

I think you want to change

while (b == true){

to use Scanner.hasNextInt() like

while (input.hasNextInt()){

If you must do a while(true) statement, which isn't good programming practice, the final code snippet would look something like this,

boolean someCondition = true;
while(true){

    if(someCondition == false){

      break;

    }

 //and here you would put something that would make that check condition eventual 
 //false to break the loop

}

This is prone too error but would work for your purposes.

That being said, the answer Elliot gave is the correct one and worth considering.

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