-1

这段代码的目的是在用户输入的二维数组中找到最大值。该代码是有道理的,但是当我尝试编译时,它给了我以下错误:

错误:

LocateLargestElement.java:41:错误:找不到符号

int 结果 = maxValue(userMatrix);
                            ^
符号:变量 userMatrix

位置:类LocateLargestElement

1 个错误

我试着和我的编程教授谈谈,但他真的很成熟,不会帮助我。基本上,我正在尝试制作resultmaxValue但它说它找不到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;
   }
}
4

3 回答 3

0

尝试这个 :

int result = maxValue(
    userMatrix(
        input,
        lengthRow(input),
        lengthColumn(input)
    )
);

你必须调用你定义的方法。

于 2014-11-13T18:02:13.720 回答
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);
}

我将把它作为练习留给读者,让它变得更好。

于 2014-11-13T18:04:07.253 回答
0

您的用户矩阵声明在哪里?您没有在出现错误的范围内声明它。

   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);
   }
于 2014-11-13T18:04:46.977 回答