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; } }
我感觉是符合你的要求的。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用