此代码的目的是在用户输入的二维数组中找到最大值。代码很有意义,但是当我尝试编译时,它会让我发现以下错误:
ERROR: LocateLargestElement.java:41: error: cannot find symbol int result = maxValue(userMatrix); ^ symbol: variable userMatrix location: class LocateLargestElement 1 error
我试着和我的编程教授谈谈,但他真的很成熟,不会帮助我。基本上,我正在努力result
maxValue
,但它说找不到userMatrix
。
//Import utility
import java.util.Scanner;
//initialize program
public class LocateLargestElement
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
int userInt = 0;
do
{
run(input);
System.out.println("Do You Want To Continue? 1 for Yes, 2 for No: ");
userInt = input.nextInt();
}
while (userInt == 1);
}
//METHOD run
public static void run (Scanner input)
{
int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR
System.out.println("The largest value in the given Matrix is: " + result);
}
//METHOD ask user for number of rows
public static int lengthRow (Scanner input)
{
System.out.println("Please enter the number of rows of the desired matrix: ");
int lengthRow = input.nextInt();
return lengthRow;
}
//METHOD ask user for number of columns
public static int lengthColumn (Scanner input)
{
System.out.println("Please enter the number of columns of the desired matrix: ");
int lengthColumn = input.nextInt();
return lengthColumn;
}
//METHOD ask user for input of values
public static int [][] userMatrix (Scanner input, int lengthRow, int lengthColumn)
{
int [][] userMatrix = new int [lengthRow][lengthColumn];
System.out.println("Please enter the values for the matrix: ");
for (int i = 0; i < lengthRow; i++)
{
for (int j = 0; j < lengthColumn; j++)
{
userMatrix[i][j] = input.nextInt();
}
}
return userMatrix;
}
//METHOD find the largest element in the matrix
public static int maxValue (int[][] userMatrix)
{
int maxValue = 0;
for (int i = 0; i < userMatrix.length; i++)
{
for (int j = 0; j < userMatrix[i].length; j++)
{
if (userMatrix[i][j] > maxValue)
{
maxValue = userMatrix[i][j];
}
}
}
return maxValue;
}
}
答案 0 :(得分:0)
试试这个:
int result = maxValue(
userMatrix(
input,
lengthRow(input),
lengthColumn(input)
)
);
您必须调用已定义的方法。
答案 1 :(得分:0)
你的第一个问题是你有一个变量和一个名为相同的方法,这会让你对使用感到困惑。这不是代码的技术问题,但对于阅读代码的任何人来说都是一个问题。
我想你可能想要这样的东西。您需要调用该方法并传递它所需的参数。由于您有获取其他值的方法,因此您应该先收集它们。这将使您的代码正常运行,但可以更好地构建。
public static void run (Scanner input)
{
int row = lengthRow(input);
int column = lengthColumn(input);
int result = maxValue(userMatrix(input, row, column));
System.out.println("The largest value in the given Matrix is: " + result);
}
我会把它作为练习让读者更好。
答案 2 :(得分:0)
您对usermatrix的声明在哪里?您没有在出现错误的范围内声明它。
public static void run (int[][] userMatrix)
{
int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR
System.out.println("The largest value in the given Matrix is: " + result);
}
或强>
public static void run (Scanner input)
{
int[][] userMatrix = new int[x][y];
int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR
System.out.println("The largest value in the given Matrix is: " + result);
}