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

我试图首先使用方法myHobbies在person类的toString中打印printHobby数组,并计算用户在printDuration中进行Hobby的总时间。我不确定为什么我不能使它正常工作并且一直在挣扎一段时间。

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

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

}

最佳答案

问题如下:

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

08-04 13:36