0

我以前曾使用此站点来帮助我完成许多编程任务,但我找不到与我现在遇到的问题类似的任何内容。

我正在尝试首先myHobbies使用该方法在 person 类的 toString 中打印数组,printHobby并计算用户在其中进行 Hobby 的总持续时间printDuration。我不知道为什么我不能让它工作并且一直在努力解决它。

任何帮助,将不胜感激。这是我的课。这是我第一次发帖,所以如果我做错了什么,请告诉我。

//--------------------Person--------------------
public class Person {
    String fName;
    String lName;
    String address;
    int age;

    String hobbyText;
    private double durationH = 0;
    private double totalDuration = 0;

    Person(String f, String l, String a, int ag) {
        fName = f;
        lName = l;
        address = a;
        age = ag;
    }

    static Hobby[] myHobbies = new Hobby[5];

    static int i = 0;

    public static void setHobby(Hobby mh) {
        myHobbies[i] = mh;
        i++;
    }

    public String toString() {
        return fName + " " + lName + " " + address + " " + age + " "
                + printDuration() + " ";
    }

    public double printDuration() {
        for (int k = 0; k < myHobbies.length; k++)
            totalDuration += myHobbies[k].getDuration();
        return totalDuration;
    }

    public String printHobbies() {
        for (int j = 0; j < myHobbies.length; j++)
            hobbyText = myHobbies[j].toString();
        return hobbyText;
    }

}


//--------------------HobbyDriver--------------------
import java.util.Scanner;

public class HobbyDriver {

    public static void main(String[] args) {

        Hobby[] newHobby = {
                new Hobby("Comics", "09/25/2012", "The Comic Store", 1),
                new Hobby("Baseball", "09/30/2012", "Fenway Park", 3),
                new Hobby("Programming", "09/212/2012", "Home", 6),
                new Hobby("Board Games", "09/01/2012", "Tom's House", 3),
                new Hobby("Watching Dr. Who", "09/27/2012", "Home", 1) };

        String personChoice;
        Scanner hobbyScan = new Scanner(System.in);
        do {

            String fName;
            String lName;
            int age;
            String address;
            String hobbyName;
            String partDate;
            String location;
            double duration;
            int userHobby;
            String hobbyChoice;

            System.out.println("What is your first name?");

            fName = hobbyScan.nextLine();

            System.out.println("What is your last name?");
            lName = hobbyScan.nextLine();

            System.out.println("What is your address?");

            address = hobbyScan.nextLine();

            System.out.println("What is your age?");

            age = hobbyScan.nextInt();
            hobbyScan.nextLine();

            do {

                System.out
                        .println("What hobbies would you like to do?\n"
                                + "choose between     Comics(0)\nBaseball(1)\nProgramming(2)\nBoard Games(3)\nWatching Dr.Who(4)\n"
                                + "\nEnter    the name of the hobby and then press enter");

                userHobby = hobbyScan.nextInt();
                hobbyScan.nextLine();

                System.out
                        .println("Would you like to add another hobby? (enter yes/no)");

                hobbyChoice = hobbyScan.nextLine();

                Person.setHobby(newHobby[userHobby]);

            } while (hobbyChoice.equals("yes"));

            System.out
                    .println("Would you like to add another person? (enter yes/no)");
            personChoice = hobbyScan.nextLine();

            int i = 0;

            Person[] newPerson = new Person[5];
            newPerson[i] = new Person(fName, lName, address, age);

            System.out.println(newPerson[i].toString());

            i++;

        } while (personChoice.equals("yes"));

    }

}




//--------------------Hobby--------------------
public class Hobby {

    private String hobbyName;
    private String partDate;
    private String location;
    private double duration;

    Hobby(String h, String p, String l, double d) {
        hobbyName = h;
        partDate = p;
        location = l;
        duration = d;

    }

    public String toString() {
        return hobbyName + " " + partDate + " " + location + " " + duration;
    }

    public void setDuration(double d) {
        d = duration;
    }

    public double getDuration() {
        return duration;
    }

}
4

2 回答 2

1

问题如下:

public String printHobbies() {
    for (int j = 0; j < myHobbies.length; j++)
        hobbyText = myHobbies[j].toString();    
    return hobbyText;
}

首先,您在每个循环中覆盖您的字符串。写

hobbyText += myHobbies[j].toString();

其次,如果你不添加 5 个爱好,你会得到一个 NPE,因为数组中的每个项目一开始都是空的。

所以你必须检查 myHobbies[j] 是否不为空:

public String printHobbies() {
    for (int j = 0; j < myHobbies.length; j++) {
        if(myHobbies[j] != null) {
            hobbyText += myHobbies[j].toString();
        }
    }  
    return hobbyText;
}

您可能还想看看 Collections:http ://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html

于 2012-09-26T14:07:12.040 回答
1

很少有错误可能导致您的代码出现问题,这取决于当前的使用情况。

  1. 您不会重置总持续时间,这意味着它仅在第一次调用后才返回正确的结果。否则,它会乘以调用次数。

    public double printDuration() {
        for (int k = 0; k < myHobbies.length; k++)
            totalDuration += myHobbies[k].getDuration();
        return totalDuration;
    }
    
  2. 您使用固定大小为 5 的简单数组。这意味着,如果您添加超过 5 个爱好,它将抛出IndexOutOfBoundsException.

  3. 您在数组中的所有爱好上调用toString()方法,尽管它们是设置的。默认情况下,数组初始化为null,这意味着,如果您设置的爱好少于 5 个,则尝试在nullwhich throws上调用它NullPointerException

    public String printHobbies() {
        for (int j = 0; j < myHobbies.length; j++)
            hobbyText += myHobbies[j].toString();
        return hobbyText;
    }
    
于 2012-09-26T14:08:12.530 回答