gpt4 book ai didi

java - 无法从其他方法返回值

转载 作者:行者123 更新时间:2023-12-01 16:49:15 25 4
gpt4 key购买 nike

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: error: cannot find symbol errorCode = getError(errorCode2); ^ symbol: variable errorCode2 location: class fourinaline_reference_edited 1 error

如何解决?

最佳答案

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

errorCode = getError(errorCode);

你的方法应该采用int、int[][]数组和int,而不仅仅是int:

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

关于java - 无法从其他方法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com