在类中创建方法时我做错了什么?

时间:2014-11-11 16:51:00

标签: java methods identifier

我正在尝试创建一个用户输入二维数组的程序,程序将在数组中找到最高值,并输出值以及相关的行和列。我在类中创建了一个方法(称为locatelargest),该方法遍历2d数组中的所有点,并将MaxValue放在一边。然后它返回MaxValue。我得到错误)和]预期,非法的类型开始,;预期和标识符。如何让整个方法和程序工作?谢谢你的时间。

import java.util.Scanner;

public class Find {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter the number of rows in the the array: ");
int numberOfRows = input.nextInt();
System.out.print ("Enter the number of rows in the the array: ");
int numberOfColumns = input.nextInt();

double[][] anarray = new double[numberOfRows][numberOfColumns];


System.out.println("Enter the array: ");
for (int i = 0; i < a.length; i++){
  for (int j = 0; j < a[i].length; j++){
    a[i][j] = input.nextDouble();
  }  
}
Location location = locatelargest(anarray);
System.out.println("The location of the largest element is "
  + location.maxValue + " at ("
  + location.row + ", " + location.column + ")");    


 }

 }

  class Location{
     public double maxValue;
     public int row;
     public int column;

        public static double[] locatelargest(double[r][c] array){

              for (int r = 0; r < array.length; r++){
                 for (int c = 0; c < array[r].length; c++){
                    if (b[row][column] > maxValue){
                       MaxValue = array[row][column];
                       row = r;
                       column = c;
                       return MaxValue;
                     }
                 }
              }
        }       
  }

更新:我让程序正常运行。这是我的代码。

import java.util.Scanner;

public class Big {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter the number of rows in the the array: ");
int numberOfRows = input.nextInt();
System.out.print ("Enter the number of rows in the the array: ");
int numberOfColumns = input.nextInt();

double[][] array = new double[numberOfRows][numberOfColumns];


System.out.println("Enter the array: ");
for (int i = 0; i < array.length; i++){
  for (int j = 0; j < array[i].length; j++){
    array[i][j] = input.nextDouble();
     }  
    }
Location location = new Location();

location.locatelargest(array);

System.out.println("The location of the largest element is "
  + location.maxValue + " at ("
  + location.row + ", " + location.column + ")");    


   }

   }

  class Location{
     public static double maxValue;
     public static int row;
     public static int column;

        public static double locatelargest(double[][] array){

              for (int r = 0; r < array.length; r++){
                 for (int c = 0; c < array[r].length; c++){
                    if (array[r][c] > maxValue){
                       maxValue = array[r][c];
                       row = r;
                       column = c;

                     }
                 }
              }
              return maxValue;
        }       
  }

1 个答案:

答案 0 :(得分:1)

帮助您入门的一些基本问题:

您的方法签名应该public static double locatelargest(double[][] array)开始。 除非您尝试返回行和列的数组,这将是int[]

你应该

   if (b[r][c] > maxValue){
        MaxValue = array[r][c];

您需要将return语句移到循环之外。您需要声明double maxValue(并且不要将变量名称大写)

您还需要将其称为Location.locatelargest(array),方法名称通常为locateLargest

TL; DR: Sotirios是对的。您需要返回并阅读手册。