CodeGym
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
PeaceLovePang
第 14 级
Warrington
  • 30.05.2020
  • 509次阅读
  • 1个评论

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

有关 任务的问题 猫之间的关系
Java 语法,  第 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
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.