java - 找不到符号-调用方法时

标签 java multidimensional-array error-handling cannot-find-symbol

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

ERROR:

LocateLargestElement.java:41: error: cannot find symbol

int result = maxValue(userMatrix);
                            ^
symbol:   variable userMatrix

location: class LocateLargestElement

1 error

I tried to talk to my programming professor, but he was being real mature and would not help me. Basically, I am trying to make result the maxValue, but it says it cannot find 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;
   }
}

最佳答案

您的第一个问题是您有一个变量和一个名称相同的方法,这会使您在使用时感到困惑。这不是代码的技术问题,但是对于任何阅读代码的人来说都是一个问题。

我想您可能打算这样。您需要调用该方法并传递其所需的参数。由于您有获取其他值的方法,因此应首先收集这些值。这将使您的代码起作用,但结构可能会更好。

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);
}

我将其作为练习使读者变得更好。

关于java - 找不到符号-调用方法时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915167/

相关文章:

java - 在这种情况下如何使用 Jackson Package?

java - 切换到特定选项卡并关闭其他选项卡

java - 树集的行为不同,但哈希集在自定义对象的情况下则不然

php - Laravel 中如何将消息传递到另一个页面?

selenium - Selenium 没有这种元素异常

java - 如何获取 mime 类型的 p7s 文件?

c - 我如何将它变成一个结构?

javascript - 将多维数组中的 HTML 表格与数据驱动文档分组

database - 如何摆脱#func!出现在MS Access的查询表中?

python-2.7 - ndarray的Numpy堆栈列表