我有这样的PetRecord类:

 public class PetRecord
{
    private String name;
    private int age;
    private int weight;
    public PetRecord(String initialName)
    {
        name = initialName;
        age = 0;
    }

    public void set(String newName)
    {
        name = newName; //age and weight are unchanged.
    }

    public PetRecord(int initialAge)
    {
        name = "No name yet.";
        weight = 0;
        if (initialAge < 0)
        {
            System.out.println("Error: Negative age.");
            System.exit(0);
        }
        else
            age = initialAge;
    }

    public void set(int newAge)
    {
        if (newAge < 0)
        {
            System.out.println("Error: Negative age.");
            System.exit(0);
        }
        else
            age = newAge;
        //name and weight are unchanged.
    }

    public PetRecord(double initialWeight)
    {
        name = "No name yet";
        age = 0;
        if (initialWeight < 0)
        {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        }
        else
            weight = initialWeight;
    }

    public void set(double newWeight)
    {
        if (newWeight < 0)
        {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        }
        else
            weight = newWeight; //name and age are unchanged.
    }

    public PetRecord()
    {
        name = "No name yet.";
        age = 0;
        weight = 0;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public double getWeight()
    {
        return weight;
    }
}


我还有另一个使用PetRecord的类,让用户输入有多少个Pet,输入Pet的名称,然后按字母顺序对数组进行排序。我已经弄清楚了排序部分(我认为),但是我在设置每个PetRecord对象的名称的循环方面遇到了麻烦。我怎样才能解决这个问题?

import java.util.Scanner;
public class PetSort {


    public static void selectionSort(PetRecord[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            // can add print statement here to help debug sorting algorithm:
            System.out.print("In selectionSort: ");
            for (int k = 0; k < a.length; k++)
                System.out.print(a[k] + ", ");
            System.out.println();

            int indexOfMin = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].getName().compareTo(a[indexOfMin].getName()) > 0)
                    indexOfMin = j;
            }
            PetRecord temp = a[i];
            a[i] = a[indexOfMin];
            a[indexOfMin] = temp;
        }
    }


    public static void main(String args[]){
        int i;
        Scanner s = new Scanner(System.in);

        System.out.println("How many pets are there?");
        i = s.nextInt();
        PetRecord[] array = new PetRecord[i];
        System.out.println("Please give the names of the pets: ");
        for (int k = 0; k < array.length; k++){
            // This is the line that I'm trying to step through the array, and set the name of each PetRecord Object to what the user inputs.
            //PetRecord array[i] = new PetRecord(s.nextLine());
        }
        selectionSort(array);


    }
}

最佳答案

我在设置每个名称的循环时遇到麻烦
  PetRecord对象。我怎样才能解决这个问题?


您需要在循环内插入println消息,否则用户可能不知道要多少次继续输入所需的数据。

另外,循环内不需要整行:

PetRecord array[i] = new PetRecord(s.nextLine());


这样做会:

array[i] = new PetRecord(s.nextLine());


注意-array的索引器是k而不是i。如果使用i在预定义数组中建立索引,则会出现IndexOutOfBoundsException异常。

例:

System.out.println("How many pets are there?");
i = s.nextInt();
PetRecord[] array = new PetRecord[i];

for (int k = 0; k < array.length; k++){
    System.out.println("Please give the names of the pets: ");
    array[k] = new PetRecord(s.nextLine());
}

07-27 17:18