【问题标题】:The method I am trying to call is not returning a value我试图调用的方法没有返回值
【发布时间】:2012-08-21 16:22:59
【问题描述】:

我正在尝试将方法的返回值分配给 int,但它给了我一个错误“找不到符号 符号:变量 columnResult"

代码如下:

public void start() {
    String inputString = "XOXOXOO     X     XXO         X     OXO   ";


    columnResult();
    int column = columnResult;

    enterToken("X", inputString,column);    

  }
  private int columnResult(){
    System.out.println("Enter column for X:");
    String keyInput = Keyboard.readInput();
    int column1 = Integer.parseInt(keyInput);
    return column1;
  }

我该如何解决?

【问题讨论】:

  • 它返回一个值就好了,但你忽略了它。您需要先将其分配给列:column = columnResult()。摆脱 columnResult 变量,因为这只会让您感到困惑。
  • 当您调用非 void 方法时,例如 columnResult(),您需要按照 @HovercraftFullOfEels 的建议将其返回值分配/保存到变量中,否则其值将丢失。

标签: java methods symbols


【解决方案1】:

试试

int column = columnResult();

而不是 列结果(); int column = columnResult;

你必须将函数的结果赋给整数。

【讨论】:

    【解决方案2】:

    你应该直接写

    int column = columnResult();
    

    因为它是一个返回 int 的方法

    【讨论】:

      【解决方案3】:

      你应该这样做

      int column = columnResult();
      
      not 
      
      int column = columnResult;
      

      【讨论】:

        【解决方案4】:
        columnResult();
        int column = columnResult;
        

        应该是

        int column = columnResult();
        

        【讨论】:

          【解决方案5】:

          改为-

          int column = columnResult();
          

          仅调用columnResult(); 而不分配返回值,您将失去它。

          【讨论】:

            猜你喜欢
            • 2015-02-15
            • 1970-01-01
            • 1970-01-01
            • 2016-04-25
            • 2021-07-27
            • 2019-11-15
            • 2021-04-02
            • 2017-08-15
            • 2012-08-05
            相关资源
            最近更新 更多