简体   繁体   中英

While loop doesn't quit

i wrote a program in java wich compares tow variables values, X and Y. when i enter the same number for X and Y in the first attempt of the loop it says Match and terminate. But if it returned "false" in the first loop and then returned "true" in the next it doesn't terminate and goes on as if "b" has a "false" value.

import java.util.Scanner;
public class clads {

    //Variables
    public static int y;
    public static int x;
    static boolean b = mymethod() ;


    //MainProcess
    public static boolean mymethod() {
        Scanner myscanner = new Scanner(System.in);
        System.out.println("put a number for X");
        x = myscanner.nextInt();
        System.out.print("put a number for Y");
        y = myscanner.nextInt();
        if (y==x){
            System.out.println("match");
            return true;
        }else{
            System.out.println("Mismatch, Redo");
            return false;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        while(b ==false){
            mymethod();
        }
    }
}

But when I added a "Break;" keyword it terminated whenever it returned a "true" value. can i have some explanation please.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    while(b ==false){
      mymethod();
      Break;
    }

When you initialize b by calling mymethod , it's set to either true or false forever. If it's true, your doesn't execute. If it's false, your loop executes forever.

Your mistake is in setting the value of b when b is declared. You actually don't need b at all. Just put the invocation of mymethod() inside the while condition:

import java.util.Scanner;
public class clads {

    //Variables
    public static int y;
    public static int x;

    //MainProcess
    public static boolean mymethod() {
        Scanner myscanner = new Scanner(System.in);
        System.out.println("put a number for X");
        x = myscanner.nextInt();
        System.out.print("put a number for Y");
        y = myscanner.nextInt();
        if (y==x){
            System.out.println("match");
            return true;
        }else{
            System.out.println("Mismatch, Redo");
            return false;
        }
    }

    public static void main(String[] args) {
        while(!mymethod());
    }
}

You have to check the return value from mymethod() every time it's invoked. Your original code just caught the first value and used it forever.

Because your variable initialize once and then never get updated. try:

public static void main(String[] args) {

    while(!b){
      b = mymethod();
    }
}

you can modify the code as follows below

public static void main(String[] args) {
// TODO Auto-generated method stub
while(b ==false){
    if (mymethod()) {
       break;
    }
}

if the function mymethod() returns true, the while loop will terminate, but when the function returns false, the while will continue.

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