gpt4 book ai didi

Java Noob 创建自定义方法来打印数组并计算持续时间

转载 作者:行者123 更新时间:2023-12-02 07:33:34 26 4
gpt4 key购买 nike

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

我尝试首先使用方法 printHobby 打印 person 类的 toString 中的 myHobbies 数组,并计算用户的总持续时间在 printDuration 中进行爱好。我不知道为什么我不能让它工作,并且已经为此苦苦挣扎了一段时间。

如有任何帮助,我们将不胜感激。这是我的类(class)。这是我第一次发帖,如果我做错了什么,请告诉我。

//--------------------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 个 Hobbies,你将会得到一个 NPE,因为数组中的每一项一开始都是 null。

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

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

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

关于Java Noob 创建自定义方法来打印数组并计算持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12603219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com