繁体   English   中英

调用toString方法时遇到问题

[英]Trouble calling the toString Method

如下所示,我的方法locateLargest()是一种在数组中查找最大值的坐标的方法。 当我尝试在我的主方法中用我的locateLargest方法调用toString方法时,出现一个错误:无法对LocationTest类型的非静态方法locateLargest(int [] [])进行静态引用。 不知道如何解决这个问题。

import java.util.Scanner;

public class LocationTest {

public static void main(String[] args) {
    Scanner numberInput = new Scanner(System.in);
    System.out.println("Enter the number of rows and columns of the array: ");
    int row = numberInput.nextInt();
    int column = numberInput.nextInt();
    Location l1 = new Location(row, column);
    Location l2 = new Location();
    row = l1.getRow();
    column = l1.getColumn();
    int[][] array = new int[l1.getRow()][l1.getColumn()];
    System.out.println("Please enter the array elements: ");
    for (int r = 0; r < array.length; r++){
        for (int c = 0; c < array[r].length; c++){
            array[r][c] = numberInput.nextInt();
        }//end nested loop

    }//end for loop
    System.out.println(getMax(array));
    System.out.println(locateLargest(array).toString());

}



    public static int getMax(int[][] x){
        int max = Integer.MIN_VALUE;;
        for (int i = 0; i < x.length; i++){
            for (int j = 0; j < x[i].length; j++){

                if (x[i][j] > max)

                    max = x[i][j];
            }
    }
        return max;
    }


    public Location locateLargest(int[][] x){ 
        int maxValue = getMax(x);
        for (int i = 0; i < x.length; i++){
            for (int j = 0; j < x.length; j++)
                if (x[i][j] == maxValue)
                    return new Location(i,j);
        }
        return null;
    }
}
class Location {
private int row;
private int column;

Location(){}//end constructor

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor

public int getRow(){
    return this.row;
}

public int getColumn(){
    return this.column;
}

public String toString(){
    return "[" + row + "][" + column + "]";
}

}

函数locateLargest()是非静态的,因此您不能从诸如main()static函数中调用它。 locateLargest()更改为static ,因为它似乎没有使用任何实例变量。

您看到编译错误的原因是不允许static函数调用非static函数。

使locateLargest(int[][] x)为静态方法。 问题是非静态方法locateLargest(int[][] x)无法从静态方法main()

public static Location locateLargest(int[][] x){ 

}

更改

public Location locateLargest(int[][] x){ 

public static Location locateLargest(int[][] x){

顺便说一句,在这种情况下,不需要调用toString() 您可以简单地写:

System.out.println(locateLargest(array));

此方法应该是静态的

public static Location locateLargest(int[][] x){ 
    int maxValue = getMax(x);
    for (int i = 0; i < x.length; i++){
        for (int j = 0; j < x.length; j++)
            if (x[i][j] == maxValue)
                return new Location(i,j);
    }
    return null;
}

static方法(在您的情况下为main )不能调用方法或方法之外的任何非static变量。 虽然,非static方法可以调用变量的static方法。

另外,就像您在getmax方法中所做的getmax ,您可能想要这样的内部循环

for (int j = 0; j < x[i].length; j++)

instead of

for (int j = 0; j < x.length; j++)

这里

public static Location locateLargest(int[][] x){ 
    int maxValue = getMax(x);
    for (int i = 0; i < x.length; i++){
        for (int j = 0; j < x[i].length; j++)
            if (x[i][j] == maxValue)
                return new Location(i,j);
    }
    return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM