CodeGym
促销
学习
课程
任务
调查 & 测验
游戏
帮助
日程
社区
Users
论坛
聊天
文章
成功故事
动态
评价
订阅
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 所有问题
CodeGym/Java 任务帮助/I don't know what the problem is, please help me.
PeaceLovePang
等级 2
Warrington
  • 2020/6/1
  • 932查看次数
  • 5评论

I don't know what the problem is, please help me.

关于任务的问题 构建家庭
Java Syntax,  等级 8,  课时 11
已解决


1.创建包含 String name、boolean sex、int age 和 ArrayList<Human> children 字段的 Human 类。
2.创建并填充对象,以便我们最终得到:两个祖父、两个祖母、一个父亲、一个母亲和三个孩子。
3.显示所有 Human 对象(提示:使用 Human 类的 toString() 方法)。

要求:
  • 程序应在屏幕上显示文本。
  • Human 类必须包含四个字段。
  • Human 类必须包含一个方法。
  • Solution 类必须包含一个方法。
  • 程序应创建对象并用数据填充它们,以得到两个祖父、两个祖母、一个父亲、一个母亲和三个孩子。然后,程序应在屏幕上显示所有 Human 对象。
package zh.codegym.task.task08.task0824; /* 构建家庭 */ import java.util.ArrayList; public class Solution { public static void main(String[] args) { //在此编写你的代码 ArrayList<Human> childrens=new ArrayList<Human>(); Human child1 = new Human("childnumberone",false,1,childrens); System.out.println( child1.toString()); Human child2 = new Human("childnumbertwo",false,1,childrens); System.out.println( child2.toString()); Human child3 = new Human("childnumberthree",false,1,childrens); System.out.println( child3.toString()); childrens.add(child1); childrens.add(child3); childrens.add(child2); Human grandfather= new Human("汤姆",false,80,childrens); System.out.println( grandfather.toString()); Human grandfather2= new Human("汤姆2",false,81,childrens); System.out.println( grandfather2.toString()); Human grandmother= new Human("珍妮",false,82,childrens); System.out.println( grandmother.toString()); Human grandmother2= new Human("珍妮2",false,83,childrens); System.out.println( grandmother2.toString()); Human father = new Human("father",true,20,childrens); System.out.println( father.toString()); Human mother = new Human("mother",false,25,childrens); System.out.println(mother.toString()); } public static class Human { //在此编写你的代码 public String name; public boolean sex; public int age; public ArrayList<Human> children; public Human(String name, boolean sex, int age, ArrayList<Human> children) { this.name = name; this.sex = sex; this.age = age; this.children = children; } public String toString() { String text = ""; text += "名字:" + this.name; text += ",性别:" + (this.sex ? "男" : "女"); text += ",年龄:" + this.age; int childCount = this.children.size(); if (childCount > 0) { text += ",孩子:" + this.children.get(0).name; for (int i = 1; i < childCount; i++) { Human child = this.children.get(i); text += "," + child.name; } } return text; } } }
0
评论 (5)
  • 热门
  • 最新
  • 最旧
您必须 登录 才能发表评论
Harry
等级 15 , Singapore, Singapore
1 六月 2020, 13:33
You may use the children instance variable to represent the parent-children relations required by the problem
0
PeaceLovePang
等级 2 , Warrington, United Kingdom
1 六月 2020, 14:36
Sorry, I still don't understand. Could you be more specific
0
Harry
等级 15 , Singapore, Singapore
1 六月 2020, 16:15解决方案
Sure, let me explain it in a more detailed way. Now the issue is that values of all children instance variables are initialized using the same ArrayList childrens, meaning that child1, child2 and child3 are the children of all Human objects. However, this does not reflect the relationships given in this problem. Suppose a couple has 2 children, Tom and Sam. In the meanwhile, Tom and Sam's grandparents are fathers and mothers of the couple. To represent these relationships, we can first create two Human objects for Tom and Sam respectively. For Tom and Sam, in the constructor we just need to initialize their children instance variables using empty ArrayList. In the second step, we create two Human objects to be the father and the mother. Since they are the parent of Tom and Sam, when initializing their instance variables children, we need to create ArrayList and add Tom and Sam to it. Only in this way, other objects can know that the father and the mother has two children, Tom and Sam. In the third step, we just need to follow the same way to create grandparents' objects. I don't know whether my explanation can make the solution a little bit more clearer.
+2
PeaceLovePang
等级 2 , Warrington, United Kingdom
2 六月 2020, 01:11
Thank you very much, I understand, and I passed through your explanation, you are a dedicated person
0
Harry
等级 15 , Singapore, Singapore
2 六月 2020, 01:59
I am glad that it helped:)
0
Learn
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 动态
  • 联盟计划
Company
  • 关于我们
  • 联系方式
  • 评价
  • 媒体中心
  • CodeGym for EDU
  • 常见问题
  • 支持
CodeGymCodeGym 是一个从零开始学习 Java 编程的在线课程。该课程是初学者掌握 Java 的完美方式。它包含 1200+ 个带即时验证的任务,以及必要的 Java 基础理论。为了帮助你在学习中取得成功,我们实现了一系列激励功能:测验、编码项目、关于高效学习的内容,以及 Java 开发者职业相关内容。
关注我们
界面语言
English Deutsch Español हिन्दी Français Português Polski বাংলা
简体中文
मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
“程序员是培养出来的,不是天生的” © 2026 CodeGym
MastercardVisa
“程序员是培养出来的,不是天生的” © 2026 CodeGym