سؤال

طريقتي locationLargest() كما هو موضح أدناه هي طريقة للعثور على إحداثيات أكبر قيمة في صفيفتي.أواجه مشكلة في وضع قيم الإرجاع في طريقة 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 () أيضا

giveacodicetagpre.

نصائح أخرى

طريقة Tostring لها رأس

giveacodicetagpre.

لذلك كل ما عليك فعله هو جعل طريقة مع هذا الرأس الذي يرجع سلسلة.

سؤالك هو غامض جدا لأن شخص ما المذكور أعلاه، والموقع ليس فئة مكتبة قياسية، لذلك لا أستطيع أن أعطيك أي شيء محدد، ولكن شيء مثل

giveacodicetagpre. قد تكون

قريبة من ما كنت تريده إذا كنت تريد أن تمثل ToString نقطة.

ماذا تحتاج لتحويلها إلى سلسلة؟ على أي حال، مهما فعلت، أضف هذا إلى النهاية:

giveacodicetagpre.

يمكنك إجراء متغير سلسلة وإرجاعه:

giveacodicetagpre.

أو مجرد طباعةها أو تفعل أي شيء آخر

giveacodicetagpre.

نأمل أن ساعد.إذا لم تخبرني عن ذلك في التعليقات!

أنظف طريقة هي وضع في صفك:

giveacodicetagpre.

لست متأكدا مما إذا كان ذلك يساعد ولكن ربما تساعد الرموز أدناه

giveacodicetagpre.

أو باستخدام string.valueof (myinteger)؛

صفك "اسمه: الموقع" تم تعيينه على عدد صحيح إذا كنت تحاول إظهارها في النافذة أو الإخراج، فحاول ذلك؟

giveacodicetagpre.

أيا كان؛ p

يجب عليك استخدام row column

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

لا يمكنك استخدام i j لانه لا i j موجود في Location.

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

طالما أن هذا (أدناه) هو مُنشئك وليس ما كان لديك من قبل xLocation و yLocation, يجب أن تكون بخير.

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top