无法从其他方法返回值

时间:2017-05-07 09:59:47

标签: java methods

import java.util.Scanner;
public class testing{

public static int getError(int inputnum, int[][] grid, int errorCode){      

    if(inputnum <0 || inputnum > grid[0].length){

        System.out.println("Range of column should be 0 to 6!");
        //change errorCode to 1 to go back to the begining of while loop
        errorCode = 1;
    }

    if(grid[5][inputnum] != 0){

        System.out.println("Column " + inputnum + " is full");
        errorCode = 1;
    }

    return errorCode;
    }

public static void main(String[] args)
{
    int player = 1;
    int errorCode = 0;
    int[][] grid = CreateGrid();
    boolean loop = true;

    drawGrid(grid);     
    Scanner input = new Scanner(System.in);


    while(loop){


    System.out.print("Player " + player + " type a column <0-6> or 9 to quit 
    current game:");

    int inputnum = input.nextInt();         
    getError(inputnum, grid, errorCode);
    errorCode = getError(errorCode);
    if (errorCode == 1)
        continue;
    }
}

我不知道为什么我无法将getError方法的错误代码传递给main方法。它说:

  

fourinaline_reference_edited.java:140:错误:找不到符号errorCode = getError(errorCode2);                              ^符号:变量errorCode2位置:类fourinaline_reference_edited 1错误

我该如何解决?

2 个答案:

答案 0 :(得分:4)

您没有将正确的类型传递给您的方法:

errorCode = getError(errorCode);

您的方法应该int, array of int[][], and int,而不仅仅是int

getError(int inputnum, int[][] grid, int errorCode)

答案 1 :(得分:1)

你为什么不这样做

errorCode = getError(inputnum,grid,errorCode);

直接?