gpt4 book ai didi

java - 家谱,无法解析 child

转载 作者:行者123 更新时间:2023-12-02 01:46:04 35 4
gpt4 key购买 nike

我对这个任务一无所知。我需要像这样制作家谱

enter image description here

但我无法解决 Main.java 中的 child 问题。对于每个名字,我都必须给 child 写 null 。

这是我的代码:Main.java 。第二个文件:Osoba.java

public class Main {

public static void main(String[] args) {

Osoba abraham = new Osoba("Abraham Simpson", null, null, null, null, null, null, null);
Osoba penelope = new Osoba("Penelope Olsen", null, null, null, null, null, null,null);
Osoba pan = new Osoba("Pan Bouvier", null, null, null, null, null,null,null);
Osoba jackie = new Osoba("Jackie Bouvier", null, null, null, null, null,null,null);
Osoba herb = new Osoba("Herb Powers", abraham, penelope, null, null, null,null,null);
Osoba homer = new Osoba("Homer Simpson", abraham, penelope, null, null, null,null,null);
Osoba marge = new Osoba("Marge Simpson", pan, jackie, null, null, null,null,null);
Osoba selma = new Osoba ("Selma Bouvier", pan, jackie, null, null, null,null,null);
Osoba bart = new Osoba("Bart Simpson", homer, abraham, penelope,marge, pan,jackie,null);

bart.sestavRodokmen();
System.out.println();
homer.sestavRodokmen();
}
}



public class Osoba {

private String name;
private Osoba father;
private Osoba dad_grandpa;
private Osoba dad_grandma;
private Osoba mother;
private Osoba mom_grandpa;
private Osoba mom_grandma;
private Osoba child;

public Osoba (String name, Osoba father, Osoba dad_grandpa, Osoba dad_grandma, Osoba mother, Osoba mom_grandpa, Osoba mom_grandma, Osoba child) {
this.name = name;
this.father = father;
this.dad_grandpa = dad_grandpa;
this.dad_grandma = dad_grandma;
this.mother = mother;
this.mom_grandpa = mom_grandpa;
this.mom_grandma = mom_grandma;
this.child = child;
}

private String rodokmen;

public void sestavRodokmen () {

System.out.println("Rodokmen pro osobu: " + name);

System.out.println("Name: " + name);
System.out.println("Father: "+ father);
System.out.println("Grandfather: "+ dad_grandpa);
System.out.println("Grandmother: "+ dad_grandma);
System.out.println("Mother: "+ mother);
System.out.println("Grandfather: "+ mom_grandpa);
System.out.println("Grandmother: "+ mom_grandma);
System.out.println("Child: "+ child);

rodokmen = "";
System.out.println(rodokmen);
}

@Override
public String toString () {
String a = "";
if(mother != null && father != null)
a += name + "\n" + father + "\n" + mother +"\n" +"\n" + child;
else
a += name;

return a;
}
}

最佳答案

在 Osaba,只需要直系 parent 和/或直系子女。

在创建新 Osaba 时,仍然无法定义父项或子项。

创作顺序的完全自由将是:

    Osoba abraham = new Osoba("Abraham Simpson");
Osoba penelope = new Osoba("Penelope Olsen");
Osoba pan = new Osoba("Pan Bouvier");
Osoba jackie = new Osoba("Jackie Bouvier");
Osoba herb = new Osoba("Herb Powers");
Osoba homer = new Osoba("Homer Simpson");
Osoba marge = new Osoba("Marge Simpson");
Osoba selma = new Osoba ("Selma Bouvier");
Osoba bart = new Osoba("Bart Simpson");

然后使用以下内容创建祖先图:

    herb.setParents(abraham, penelope);
martha.setParents(null, penelope); // If no father.

现在如果你想检查所有Osaba:是否有人没有 parent ,有人不是他自己的祖先等等,也许你需要一个容器类:

public class Osabas {
private List<Osaba> persons = new ArrayList<>();

public Osaba createOsaba(String name) {
Osaba osaba = new Osaba(name);
persons.add(osaba);
return osaba;
}

public void drawAncestryDiagram() {
...
}
}

祝你好运!

关于java - 家谱,无法解析 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53653749/

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