我的方法locateLargest()如下所示,是一种查找数组中最大值的坐标的方法。我在将返回值放入 toString 方法时遇到问题。我不知道如何将其格式化为 toString 方法。

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

我的 toString 尝试:

public String toString(){
    return "[" + i + "][" + j + "]";
}

位置类代码:

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

这是我的程序的完整代码:

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(l1.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 + "]";
    }
}
有帮助吗?

解决方案

如果位置是您定义的类,则应实现功能ToString()以及

class Location{
    int locationX,locationY;    

    public Location(int x,int y){
        locationX = x;
        locationY = y;
    }

    public String toString(){
        return locationX+","+locationY;
    }
}
.

其他提示

ToString方法具有标题

public String toString()
. 所以你需要做的就是用返回字符串的标题进行方法。

你的问题很模糊,因为上面的人指出,位置不是标准的库类,所以我不能给你任何特定的东西,但是像

这样的东西
public String toString() {
    return "(" + x + ", " + y + ")";
}
.

如果您希望ToString代表一个点,可能会接近您要完成的内容。

您需要转换为字符串? 无论如何,无论您做什么,将其添加到末尾:

.toString();
.

您可以制作一个字符串变量并返回它:

StringVariable = <object>.toString();
.

或只是打印它或做任何其他事情

System.out.println(<object>.toString());
.

希望帮助。如果它没有在评论中告诉我它!

最干净的方式是放入您的课程:

public String toString(){
        return String.format("[%i][%i]",row,column);
    }
.

我不确定这是否有帮助,但也许下面的代码有助于

  return new Location(i,j);
 String toStringI = Integer.toString(i); 
    String ToStringj = integer.toString(j);
.

或使用 string.valueof(myinteger);

如果您尝试在窗口或输出中显示它,则将其类“命名:位置”设置为Integer,尝试此操作?

System.out.println("",i,"",j);
.

无论如何; p

你应该使用 row column

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

你不能使用 i j 因为没有 i j 存在于 Location.

public String toString(){
    return "[" + i + "][" + j + "]";
}

只要这是你的构造函数而不是你之前的构造函数 xLocationyLocation, ,你应该没问题。

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top