CodeGym
促销
学习
课程
任务
调查 & 测验
游戏
帮助
日程
社区
Users
论坛
聊天
文章
成功故事
动态
评价
订阅
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 所有问题
CodeGym/Java 任务帮助/There is a problem with the output statement and it has b...
PeaceLovePang
等级 2
Warrington
  • 2020/5/30
  • 1477查看次数
  • 1评论

There is a problem with the output statement and it has been unable to pass

关于任务的问题 猫之间的关系
Java Syntax,  等级 6,  课时 11
已归档


任务:每只猫都有名字和母亲。
创建描述此情况的类。
创建两个对象:猫女儿和猫母亲。
在屏幕上显示它们。

新任务:每只猫都有名字、父亲和母亲。
编辑 Cat 以使其反映这些关系。
创建 6 个对象:祖父(父亲的父亲)、外祖母(母亲的母亲)、父亲、母亲、儿子、女儿。
按以下顺序显示所有对象:祖父、外祖母、父亲、母亲、儿子、女儿。

示例输入:
祖父泰格
外祖母普斯
父亲奥斯卡
母亲米西
儿子辛巴
女儿可可

示例输出:
猫的名字为祖父泰格,无母亲,无父亲
猫的名字为外祖母普斯,无母亲,无父亲
猫的名字为父亲奥斯卡,无母亲,祖父泰格是父亲
猫的名字为母亲米西,外祖母普斯是母亲,无父亲
猫的名字为儿子辛巴,母亲米西是母亲,父亲奥斯卡是父亲
猫的名字为女儿可可,母亲米西是母亲,父亲奥斯卡是父亲

要求:
  • 程序应以指定顺序读取 6 只猫的名字。
  • main 方法应创建 6 个 Cat 对象。
  • 程序应显示 6 行有关猫的信息。
  • 有关祖父的行(第一行)必须符合条件。
  • 有关外祖母的行(第二行)必须符合条件。
  • 有关父亲的行(第三行)必须符合条件。
  • 有关母亲的行(第四行)必须符合条件。
  • 有关儿子的行(第五行)必须符合条件。
  • 有关女儿的行(第六行)必须符合条件。
package zh.codegym.task.task06.task0621; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* 猫之间的关系 */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String grandfather=reader.readLine(); Cat catgreadfather = new Cat(grandfather); String grandmother = reader.readLine(); Cat catgrandmother = new Cat(grandmother); String father = reader.readLine(); Cat catfather = new Cat(father,null,catgreadfather); String motherName = reader.readLine(); Cat catMother = new Cat(motherName,catgrandmother,null); String son= reader.readLine(); Cat catson = new Cat(son,catMother,catfather); String daughterName = reader.readLine(); Cat catDaughter = new Cat(daughterName, catMother,catfather); System.out.println(catgreadfather); System.out.println(catgrandmother); System.out.println(catfather); System.out.println(catMother); System.out.println(catson); System.out.println(catDaughter); } public static class Cat { private String name; private Cat Mother; private Cat Father; Cat(String name) { this.name = name; } Cat(String name, Cat Mother ) { this.name = name; this.Mother = Mother; } Cat(String name,Cat Mother,Cat Father) { this.name=name; this.Mother=Mother; this.Father=Father; } @Override public String toString() { if (Mother == null && Father == null) return "猫的名字为" + name + ",无母亲,无父亲"; else if (Mother == null) return "猫的名字为" + name + ",无母亲," + Father.name + "是父亲"; else if (Father == null) return "猫的名字为" + name + "," + Mother.name + "是母亲,无父亲"; else return "猫的名字为" + name + ", " + Mother.name + "是母亲," + Father.name + " 是父亲"; } } }
0
评论 (1)
  • 热门
  • 最新
  • 最旧
您必须 登录 才能发表评论
pyw
等级 8 , Changzhi, China
27 六月 2020, 02:52
if ((monther == null) && (father == null)) return "猫的名字为" + name + ",无母亲, 无父亲"; else if(monther == null) return "猫的名字为" + name + ",无母亲"+","+father.name+"是父亲"; else if (father == null) return "猫的名字为" + name + ","+monther.name+"是母亲"+", 无父亲"; else return "猫的名字为" + name + "," + monther.name + "是母亲" + ","+ father.name + "是父亲";
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