このコードの目的は、ユーザーが入力した 2 次元配列の最大値を見つけることです。コードは理にかなっていますが、コンパイルしようとすると、次のエラーが発生します。
エラー: LocateLargestElement.java:41: エラー: シンボルが見つかりません int 結果 = maxValue(userMatrix); ^ シンボル: 変数 userMatrix 場所: クラス LocateLargestElement 1 エラー
私はプログラミングの教授と話そうとしましたが、彼は本当に成熟していて、私を助けてくれませんでした. 基本的に を作ろうとして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;
}
}