package Excesise;
public class Animal {
protected int id;
protected String name;
protected int food;
protected int happiness;
public Animal(){}
public Animal(int id,String name,int food){
this.id = id;
this.name = name;
this.food = food;
happiness = 100;
}
public void full(int food){
if (food == 100) {
System.out.println("吃饱了,睡觉觉,谢谢主人^_^");
} else if(food > 100){
System.out.println("吃不下那么多啦~");
}
}
public void happy(int happiness){
if(happiness == 100){
System.out.println(name + "说:幸福感满满☺");
} else if (happiness > 0 ){
System.out.println(name + ":再来点☹");
} else if (happiness <= 0) {
System.out.println(name + "悲伤地坐在小角落心碎☹");
}
}
public void unhappy(int food){
if (food < 50) {
System.out.println("我饿了");
} else if (food > 0){
System.out.println("快要饿死了");
} else {
System.out.println("你的宠物已经饿死了");
}
}
}
创建了一个动物的超类
package Excesise;
public class Cat extends Animal{
public Cat(int id,String name,int food){
this.id = id;
this.name = name;
this.food = food;
happiness = 50;
}
public void eat(int food){
this.food = food;
happiness += food;
}
}
又是一个小猫的派生类
package Excesise;
/*设计宠物类,用户可以自由选择养猫还是养狗,
可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度*/
import java.util.Scanner;
public class AnimalTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要领养的宠物的序号:1、猫,2、狗");
int id = scanner.nextInt();
switch (id){
case 1:
cat(id);
break;
case 2:
cat(id);
default:
System.out.println("这个小动物还没来哦~请再等等~~");
}
}
public static void cat(int id){
System.out.println("给宠物起个名字吧~");
Scanner scanner1 = new Scanner(System.in);
String name = scanner1.nextLine();
System.out.println("恭喜你领养" + name + "成功啦~快买点食物喂喂它吧~");
System.out.println("想买多少食物呢?~1-100哦~");
int food = scanner1.nextInt();
Cat cat = new Cat(id,name,food);
int food1 = 0;
int full = food + food1;
if(full <= 0){
cat.happy(full);
}
while(full > 0 && full <= 100){//50 60
if (full == 100){
cat.full(full);
break;
} else {
cat.eat(full);//50 60
cat.happy(cat.happiness);
System.out.println("再来点食物吧~");
food1 = scanner1.nextInt();//10 20
full = full + food1;//60 80
if (full == 100){
cat.full(full);
break;
} else if(full < 100){
cat.eat(full);//60
cat.happy(cat.happiness);
System.out.println(name + "吃的很开心");
continue;
}
}
if (full == 100){
cat.happy(full);
break;
}
}
}
}
功能简单了点,用一个派生类就做出来了,可是还有个小狗类,感觉这个互动差不多啊,还是我没有弄明白题目的意思?
相关知识
设计宠物类,用户可以自由选择养猫还是养狗,可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度
宠物人工智能助手的用户接受度与效益研究.docx
在《人人宠》中如何提高宠物的快乐度?
2022有什么可以在手机上养宠物的软件 电子宠物软件排行
宠物类手游排行榜
猫毛过敏?养狗怕溜?你可以养豹纹守宫呀~
有哪些经典的宠物类手游2022 可以养宠物的游戏盘点
小红书宠物类爆文笔记该怎么写
养猫可以不用猫爬架吗
常见的宠物类app定制开发竞品分析
网址: 设计宠物类,用户可以自由选择养猫还是养狗,可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度 https://m.mcbbbk.com/newsview79034.html
上一篇: 博客宠物最新资讯 |
下一篇: 有一个可以养宠物的博客,大家帮我 |