sss123158 2021-03-17 18:55 采纳率: 100%
浏览 16
已采纳

如何使用有参构造方法完成图片中的内容?

 

  • 写回答

1条回答 默认 最新

  • qq_34370249 2021-03-17 19:47
    关注
    public class Demo {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("欢迎来到宠物狗商店!");
            System.out.print("请输入要领养宠物的名字: ");
            String nickName = scanner.next();
            System.out.print("请输入狗狗的品种(1.聪明的拉布拉多犬 2.酷酷的雪瑞纳)");
            Integer type = scanner.nextInt();
            System.out.print("请输入狗狗的健康值:");
            Integer health = scanner.nextInt();
            if(health < 20){
                health = 20;
                System.out.println("健康值应该在20-100之间");
            } else if(health > 100){
                health = 100;
                System.out.println("健康值应该在20-100之间");
            }
            System.out.println(new Dog(nickName, type, health).toString());
        }
    }
    
    class Dog{
        private String nickName;
        private Integer type;
        private Integer health;
    
        public Dog(String nickName, Integer type, Integer health) {
            this.nickName = nickName;
            this.type = type;
            this.health = health;
        }
    
        @Override
        public String toString() {
            String dogType = "酷酷的雪瑞纳";
            if(type == 1){
                dogType = "聪明的拉布拉多犬";
            }
            return "我的名字叫" + nickName + ", 我的健康值是" + health + ", 我是一只" + dogType;
        }
    }
    

    我感觉是符合你的要求的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料