質問

マイメソッドLocATELARJUTSER()以下のように、マイアレイの最大の値の座標を見つける方法です。戻り値を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 + ")";
}
.

は、あなたがポイントを表すために足首が欲しいならば、あなたが行っていることに近づくかもしれません。

文字列に変換する必要がありますか? とにかく、あなたがするものは何でも、これをendに追加します。

.toString();
.

文字列変数を作成して返すことができます。

StringVariable = <object>.toString();
.

またはそれを印刷するか、他の何かをする

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

それが助けたことを願っています。コメントの中でそれについて私に言わなかったら!

最もきれいな方法はあなたのクラスに入れることです:

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

imはそれが役立つかどうかわからないが多分以下のコードは

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

または使用することによって string.valueof(myinteger);

あなたのクラス "名前:location"は整数に設定されていますあなたがそれをウィンドウで表示しようとしているか、それを試してみようとしている場合は整数に設定されていますか?

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

何でも; p

あなたのShoulrd row column

を使用
public String toString(){
    return "[" + row + "][" + column + "]";
}
.

ij 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