简体   繁体   English

While循环不会退出

[英]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. 我用java编写了一个程序,比较两个变量的值X和Y。当我在循环的第一次尝试中为X和Y输入相同的数字时,它说匹配并终止。 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. 但是,如果它在第一个循环中返回“ false”,然后在下一个循环中返回“ true”,则它不会终止,并且继续进行,就好像“ b”具有“ false”值一样。

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;" 但是当我添加“ Break”时 keyword it terminated whenever it returned a "true" value. 关键字在返回“ true”值时终止。 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. 通过调用mymethod初始化b ,它将永远设置为truefalse 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. 您的错误是在声明b时设置b的值。 You actually don't need b at all. 实际上,您根本不需要b Just put the invocation of mymethod() inside the while condition: 只需将mymethod()的调用放在while条件中:

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. 您必须在每次调用mymethod()时检查返回值。 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. 如果函数mymethod()返回true,则while循环将终止,但是当函数返回false时, while将继续。

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

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