如何从while循环返回2个布尔值? Java的

时间:2022-09-10 13:38:20

I try to return both correctPiece and correctDest but

我尝试返回correctPiece和correctDest但是

return correctPiece;

gets underlined and the error "Unreachable code" appears.

加下划线并出现错误“无法访问的代码”。

How can I return both?

我怎么能两个都回来?

   while(correctPiece && !correctDest) {

            System.out.println("Click on a destination");

            toXCo = s.getToXInt();
            toYCo = s.getToYInt();

            Move found = null;

            for( Move m : moves){
                //checks if move can be done
                if (m.ToX() == toXCo && m.ToY() == toYCo){
                    //if move is allowed- exit loop
                    found = m; 
                    correctDest = true;
                }
            }

            if (found == null) {
                //if move can't be, ask for new co-ordinates
                System.out.println("This move is not legal \n");
                    correctDest = false;
                    correctPiece = false;
            }   
            return correctDest;
            return correctPiece;
        }

6 个解决方案

#1


By changing your return type to boolean[] and using something like

通过将返回类型更改为boolean []并使用类似的方法

return new boolean[] { correctDest, correctPiece };

#2


You can make a Class that contains both the Booleans that you want to return, and create an object of this class and return that.

您可以创建一个包含要返回的布尔值的类,并创建此类的对象并返回该类。

The reason this approach is better because incase you want to extend the response in future, to add a couple of more parameters, it always is easier to do that if you have an object being returned.

这种方法更好的原因是因为如果你想在将来扩展响应,添加几个参数,如果你有一个返回的对象,它总是更容易做到这一点。

You code in this case would look like

你在这种情况下的代码看起来像

while(correctPiece && !correctDest) {

        System.out.println("Click on a destination");

        toXCo = s.getToXInt();
        toYCo = s.getToYInt();

        Move found = null;

        for( Move m : moves){
            //checks if move can be done
            if (m.ToX() == toXCo && m.ToY() == toYCo){
                //if move is allowed- exit loop
                found = m; 
                correctDest = true;
            }
        }

        if (found == null) {
            //if move can't be, ask for new co-ordinates
            System.out.println("This move is not legal \n");
                correctDest = false;
                correctPiece = false;
        }   
        return new Response(correctDest, correctPiece);
    }

And you can create a Response class to capture both the values like

您可以创建一个Response类来捕获两个值

private static class Response {

    boolean correctDest;
    boolean correctPiece;
    public Response(boolean correctDest, boolean correctPiece) {
        super();
        this.correctDest = correctDest;
        this.correctPiece = correctPiece;
    }


}

#3


You can change your return type to boolean[] so that you can return 2 boolean values.

您可以将返回类型更改为boolean [],以便返回2个布尔值。

#4


"return" terminates your method and returns the value. Everything below return is not reachable.

“return”终止您的方法并返回值。返回的所有内容都无法访问。

Try changing return type to bolean[], and change you code to something like this:

尝试将返回类型更改为bolean [],并将代码更改为以下内容:

boolean temp[] = {correctDest, correctPrice};
return temp;

#5


Return keyword exits your method, so when it first hits the return correctDest; it will exit your method. From what I see, you can modify that code to:

返回关键字退出您的方法,因此当它首次点击返回correctDest时;它会退出你的方法。从我看到的,你可以修改该代码:

if (m.ToX() == toXCo && m.ToY() == toYCo){ 
    return true;
}

and instead of return correctDest; and return correctPiece; just return false. You don't really need to return both values.

而不是返回correctDest;并返回correctPiece;只是返回false。你真的不需要返回两个值。

if (found == null)
   return false;

then just go like

然后就去吧

 correctDest = methodreturn();
 correctPiece = methodreturn(); // since it's true in order to enter the loop

Other method:

public class Bols{ 
private boolean corDes; 
private boolean corPiece; 
add getters and create a constructor
public Bols(boolean corDes, boolean corPiece){ 
this.corDes = corDes; this.corPiece = corPiece;
} 
}

Then create

Bols object = new Bols(boolean correctDest, boolean correctPiece);
return Bols;

use getters to retrieve your booleans;

用吸气剂来检索你的布尔值;

#6


When I want to return several values, e.g. a complex result, i would create a new class for it. This makes code easier to read.

当我想要返回几个值时,例如一个复杂的结果,我会为它创建一个新类。这使代码更容易阅读。

Besides that, your code is quite a mess, because:

除此之外,你的代码非常混乱,因为:

  • correctPiece is not changed at all in the loop
  • 在循环中,correctPiece根本没有改变

  • the resetting of correctDest in the if() block is redundant and the resetting of correctPiece is not necessary or maybe even buggy, depending on the logic outside the while()
  • 在if()块中重置correctDest是多余的,并且不需要重置correctPiece,甚至可能有错误,这取决于while()之外的逻辑

  • found is not needed at all
  • 发现根本不需要

  • the return statement should be outside of the loop; in your example the return statement will be executed with the first loop and the user won't be given a second chance to click on a destination
  • return语句应该在循环之外;在您的示例中,将使用第一个循环执行return语句,并且不会给用户第二次单击目标的机会

My proposal for the code is like this

我对代码的建议是这样的

////
public class UserInputValidationResult {
    boolean correctPiece;
    boolean correctDest;

    public UserInputValidationResult(boolean correctPiece, boolean correctDest) {
        this.correctPiece = correctPiece;
        this.correctDest  = correctDest;
    }

    public boolean getCorrectPiece() {
        return correctPiece;
    }

    public boolean getCorrectDest() {
        return correctDest;
    }
}
/////

// I suppose this happens somewhere before the while()
correctPiece = true;
correctDest  = false;

while(correctPiece && !correctDest) {

    System.out.println("Click on a destination");

    toXCo = s.getToXInt();
    toYCo = s.getToYInt();

    Move found = null;

    for( Move m : moves){
        //checks if move can be done
        if (m.ToX() == toXCo && m.ToY() == toYCo){
            //if move is allowed- exit loop
            found = m; 
            correctDest = true;
        }
    }

    if (found == null) {
        //if move can't be, ask for new co-ordinates
        System.out.println("This move is not legal \n");
    }
}

return new UserInputValidationResult(correctPiece, correctDest);

but I am quite uncertain if it is the final version of the code which you are looking for.

但我很不确定它是否是您正在寻找的代码的最终版本。

If you are new to programming and learning about loops, functions, datatypes and return statements, try with something simpler in the beginning. Try to split complex statements into shorter ones. For example, move the inner loop into a function:

如果您不熟悉编程和学习循环,函数,数据类型和返回语句,请在开头尝试更简单的方法。尝试将复杂语句拆分为较短语句。例如,将内循环移动到一个函数中:

private boolean isCorrectDest(toXCo, toYCo) {
    boolean result = false;

    Move found = null;
    for( Move m : moves){
        //checks if move can be done
        if (m.ToX() == toXCo && m.ToY() == toYCo){
            //if move is allowed- exit loop
            found = m; 
            result = true;
            break;
        }
    }

    return result; // and why did we define found?
}

while(correctPiece && !correctDest) {

    System.out.println("Click on a destination");
    toXCo = s.getToXInt();
    toYCo = s.getToYInt();

    correctDest = isCorrectDest(toXCo, toYCo);
    if (!correctDest) {
        //if move can't be, ask for new co-ordinates
        System.out.println("This move is not legal \n");
    }
}

#1


By changing your return type to boolean[] and using something like

通过将返回类型更改为boolean []并使用类似的方法

return new boolean[] { correctDest, correctPiece };

#2


You can make a Class that contains both the Booleans that you want to return, and create an object of this class and return that.

您可以创建一个包含要返回的布尔值的类,并创建此类的对象并返回该类。

The reason this approach is better because incase you want to extend the response in future, to add a couple of more parameters, it always is easier to do that if you have an object being returned.

这种方法更好的原因是因为如果你想在将来扩展响应,添加几个参数,如果你有一个返回的对象,它总是更容易做到这一点。

You code in this case would look like

你在这种情况下的代码看起来像

while(correctPiece && !correctDest) {

        System.out.println("Click on a destination");

        toXCo = s.getToXInt();
        toYCo = s.getToYInt();

        Move found = null;

        for( Move m : moves){
            //checks if move can be done
            if (m.ToX() == toXCo && m.ToY() == toYCo){
                //if move is allowed- exit loop
                found = m; 
                correctDest = true;
            }
        }

        if (found == null) {
            //if move can't be, ask for new co-ordinates
            System.out.println("This move is not legal \n");
                correctDest = false;
                correctPiece = false;
        }   
        return new Response(correctDest, correctPiece);
    }

And you can create a Response class to capture both the values like

您可以创建一个Response类来捕获两个值

private static class Response {

    boolean correctDest;
    boolean correctPiece;
    public Response(boolean correctDest, boolean correctPiece) {
        super();
        this.correctDest = correctDest;
        this.correctPiece = correctPiece;
    }


}

#3


You can change your return type to boolean[] so that you can return 2 boolean values.

您可以将返回类型更改为boolean [],以便返回2个布尔值。

#4


"return" terminates your method and returns the value. Everything below return is not reachable.

“return”终止您的方法并返回值。返回的所有内容都无法访问。

Try changing return type to bolean[], and change you code to something like this:

尝试将返回类型更改为bolean [],并将代码更改为以下内容:

boolean temp[] = {correctDest, correctPrice};
return temp;

#5


Return keyword exits your method, so when it first hits the return correctDest; it will exit your method. From what I see, you can modify that code to:

返回关键字退出您的方法,因此当它首次点击返回correctDest时;它会退出你的方法。从我看到的,你可以修改该代码:

if (m.ToX() == toXCo && m.ToY() == toYCo){ 
    return true;
}

and instead of return correctDest; and return correctPiece; just return false. You don't really need to return both values.

而不是返回correctDest;并返回correctPiece;只是返回false。你真的不需要返回两个值。

if (found == null)
   return false;

then just go like

然后就去吧

 correctDest = methodreturn();
 correctPiece = methodreturn(); // since it's true in order to enter the loop

Other method:

public class Bols{ 
private boolean corDes; 
private boolean corPiece; 
add getters and create a constructor
public Bols(boolean corDes, boolean corPiece){ 
this.corDes = corDes; this.corPiece = corPiece;
} 
}

Then create

Bols object = new Bols(boolean correctDest, boolean correctPiece);
return Bols;

use getters to retrieve your booleans;

用吸气剂来检索你的布尔值;

#6


When I want to return several values, e.g. a complex result, i would create a new class for it. This makes code easier to read.

当我想要返回几个值时,例如一个复杂的结果,我会为它创建一个新类。这使代码更容易阅读。

Besides that, your code is quite a mess, because:

除此之外,你的代码非常混乱,因为:

  • correctPiece is not changed at all in the loop
  • 在循环中,correctPiece根本没有改变

  • the resetting of correctDest in the if() block is redundant and the resetting of correctPiece is not necessary or maybe even buggy, depending on the logic outside the while()
  • 在if()块中重置correctDest是多余的,并且不需要重置correctPiece,甚至可能有错误,这取决于while()之外的逻辑

  • found is not needed at all
  • 发现根本不需要

  • the return statement should be outside of the loop; in your example the return statement will be executed with the first loop and the user won't be given a second chance to click on a destination
  • return语句应该在循环之外;在您的示例中,将使用第一个循环执行return语句,并且不会给用户第二次单击目标的机会

My proposal for the code is like this

我对代码的建议是这样的

////
public class UserInputValidationResult {
    boolean correctPiece;
    boolean correctDest;

    public UserInputValidationResult(boolean correctPiece, boolean correctDest) {
        this.correctPiece = correctPiece;
        this.correctDest  = correctDest;
    }

    public boolean getCorrectPiece() {
        return correctPiece;
    }

    public boolean getCorrectDest() {
        return correctDest;
    }
}
/////

// I suppose this happens somewhere before the while()
correctPiece = true;
correctDest  = false;

while(correctPiece && !correctDest) {

    System.out.println("Click on a destination");

    toXCo = s.getToXInt();
    toYCo = s.getToYInt();

    Move found = null;

    for( Move m : moves){
        //checks if move can be done
        if (m.ToX() == toXCo && m.ToY() == toYCo){
            //if move is allowed- exit loop
            found = m; 
            correctDest = true;
        }
    }

    if (found == null) {
        //if move can't be, ask for new co-ordinates
        System.out.println("This move is not legal \n");
    }
}

return new UserInputValidationResult(correctPiece, correctDest);

but I am quite uncertain if it is the final version of the code which you are looking for.

但我很不确定它是否是您正在寻找的代码的最终版本。

If you are new to programming and learning about loops, functions, datatypes and return statements, try with something simpler in the beginning. Try to split complex statements into shorter ones. For example, move the inner loop into a function:

如果您不熟悉编程和学习循环,函数,数据类型和返回语句,请在开头尝试更简单的方法。尝试将复杂语句拆分为较短语句。例如,将内循环移动到一个函数中:

private boolean isCorrectDest(toXCo, toYCo) {
    boolean result = false;

    Move found = null;
    for( Move m : moves){
        //checks if move can be done
        if (m.ToX() == toXCo && m.ToY() == toYCo){
            //if move is allowed- exit loop
            found = m; 
            result = true;
            break;
        }
    }

    return result; // and why did we define found?
}

while(correctPiece && !correctDest) {

    System.out.println("Click on a destination");
    toXCo = s.getToXInt();
    toYCo = s.getToYInt();

    correctDest = isCorrectDest(toXCo, toYCo);
    if (!correctDest) {
        //if move can't be, ask for new co-ordinates
        System.out.println("This move is not legal \n");
    }
}