【问题标题】:How do i return 2 boolean values from a while loop? Java如何从 while 循环中返回 2 个布尔值?爪哇
【发布时间】:2015-07-20 14:13:38
【问题描述】:

我尝试返回 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;
        }

【问题讨论】:

  • 你不能从一个函数返回多个值。为确保这两个值都反映到您调用的位置,请通过引用传递它们。

标签: java while-loop boolean return


【解决方案1】:

通过将您的 return 类型更改为 boolean[] 并使用类似的东西

return new boolean[] { correctDest, correctPiece };

【讨论】:

    【解决方案2】:

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

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

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

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

    您可以创建一个响应类来捕获两个值,例如

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

    【讨论】:

    • 这是最好的解决方案。如果你用一些代码演示了返回值类及其使用的例子,我会认为它是最好的答案。
    • 当然。添加了一个示例。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      "return" 终止你的方法并返回值。 return 以下的所有内容都无法访问。

      尝试将返回类型更改为 bolean[],并将您的代码更改为如下所示:

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

      【讨论】:

        【解决方案5】:

        Return 关键字退出你的方法,所以当它第一次遇到 return correctDest; 它会退出你的方法。据我所知,您可以将该代码修改为:

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

        而不是返回正确的目的地;并返回正确的片段;只是返回假。 您实际上不需要返回这两个值。

        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 检索您的布尔值;

        【讨论】:

          【解决方案6】:

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

          除此之外,你的代码很乱,因为:

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

          【讨论】:

            猜你喜欢
            • 2023-03-04
            • 1970-01-01
            • 2011-08-01
            • 2020-06-04
            • 2016-01-30
            • 2017-11-20
            • 2018-05-24
            • 2018-05-20
            • 2020-12-03
            相关资源
            最近更新 更多