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

时间:2015-05-09 20:15:13

标签: java while-loop boolean return

我尝试返回correctPiece和correctDest但

return correctPiece;

加下划线和错误"无法访问代码"出现。

我怎样才能同时返回?

   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 个答案:

答案 0 :(得分:5)

return类型更改为boolean[]并使用类似

的内容
return new boolean[] { correctDest, correctPiece };

答案 1 :(得分:4)

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

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

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

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);
    }

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

这样的值
private static class Response {

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


}

答案 2 :(得分:2)

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

答案 3 :(得分:2)

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

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

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

答案 4 :(得分:2)

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

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

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

if (found == null)
   return false;

然后就像

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

其他方法:

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;
} 
}

然后创建

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

使用getter来检索你的布尔值;

答案 5 :(得分:1)

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

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

  • correctPiece 在循环中根本没有改变
  • 在if()块中重置 correctDest 是多余的,并且 correctPiece 的重置不是必需的,甚至可能是错误的,这取决于while之外的逻辑( )
  • 发现根本不需要
  • return语句应该在循环之外;在你的例子中,return语句将在第一个循环中执行,用户不会有第二次机会点击目的地

我对代码的建议是这样的

////
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);

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

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

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");
    }
}