Java小程序——petshop
来源:互联网 发布:java unix timestamp 编辑:程序博客网 时间:2024/06/05 09:14
《Java小程序——petshop》
1.描述:刚学Java写的小程序,用到类的封装,使用。
2.工具:eclipse.exe
3.功能:吃,玩。
/*****************************************************Author:Ivan Version:0.1 Date: File name:Pet.javaDescription:类*****************************************************/public class Pet {//成员变量(静态属性)private String strain; //宠物学名private String name; //宠物名字(小名)private String sex; //宠物性别private int old = 0; //宠物等级(进化用)private int health = 99; //宠物健康(<0会死)private int eat = 99; //宠物饱食度(<0会死)private int love = 50; //宠物亲密度//对所有私有成员变量进行封装public String getStrain(){return strain;}public void setStrain(String strain) {this.strain = strain;}public String getName() {return name;}public void setName(String name){this.name = name;}public String getSex() {return sex;}public void setSex(String sex){this.sex = sex;}public int getOld() {return old;}public void setOld(int old){if(old < 0 || old > 100){this.old = 0;System.out.println("宠物的等级越界,默认为0");}else{this.old = old;}}public int getHealth() {return health;}public void setHealth(int health){if(health < 0 || health > 100){this.health = 70;System.out.println("宠物的健康越界,默认为70");}else{this.health = health;}}public int getEat(){return eat;}public void setEat(int eat){if(eat < 0 || eat > 100){this.eat = 70;System.out.println("宠物的饱食度越界,默认为70");}else{this.eat = eat;}}public int getLove(){return love;}public void setLove(int love) {this.love = love;}//封装的函数(动态行为)public void print(){System.out.println("宠物学名:"+strain);System.out.println("宠物名字(小名):"+name);System.out.println("宠物性别:"+sex);//System.out.println("宠物等级(进化用):"+old);System.out.println("宠物健康(<0会死):"+health);System.out.println("宠物饱食度(<0会死):"+eat);System.out.println("宠物亲密度:"+love);}public void eat(){if(eat >100){this.eat = this.eat + 3; this.love = this.love -3;this.health = this.health -3;System.out.println("宠物吃撑了!");System.out.println("当前饱食度:"+eat+"当前亲密度:"+love+"当前健康值:"+health);}else{this.eat = this.eat + 3; this.love = this.love + 3;System.out.println("宠物正在吃饭!");System.out.println("当前饱食度:"+eat+"当前亲密度:"+love+"当前健康值:"+health);}if(eat > 120){System.out.println("宠物吃死了!");this.love = 0;}if(health < 10){System.out.println("宠物病死了!");this.love = 0;}if(love < 10){System.out.println("宠物不跟你玩了!");this.love = 0;}}public void play(){if(health < 60){this.eat = this.eat - 3; this.love = this.love -3;this.health = this.health + 5;System.out.println("宠物生病了!");System.out.println("当前饱食度:"+eat+"当前亲密度:"+love+"当前健康值:"+health);}else{this.eat = this.eat - 5; this.love = this.love + 3;this.health = this.health + 5;System.out.println("宠物玩的很开心!");System.out.println("当前饱食度:"+eat+"当前亲密度:"+love+"当前健康值:"+health);}if(health > 120){this.health = 120;}if(eat < 10){System.out.println("宠物玩死了!");this.love = 0;}if(health < 10){System.out.println("宠物病死了!");this.love = 0;}if(love < 10){System.out.println("宠物不跟你玩了!");this.love = 0;}}}
/*****************************************************Author:Ivan Version:0.1 Date: File name:Shop.javaDescription:main*****************************************************/import java.util.Scanner;public class Shop {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPet pet1 = new Pet();Scanner sc = new Scanner(System.in);int chose;String pet;System.out.println("欢迎您来到宠物店 ");System.out.println("请输入要领养宠物(1.皮卡丘 2.卡比兽):");chose = sc.nextInt();if(chose == 1){pet1.setStrain("皮卡丘");}else{pet1.setStrain("卡比兽");}System.out.println("请给你的宠物取个名字吧:");pet = sc.nextLine();String petname = sc.nextLine();pet1.setName(petname);System.out.println("你希望你的宠物性别为(1.Q仔 2.Q妹):");chose = sc.nextInt();if(chose == 1){pet1.setSex("Q仔");}else{pet1.setSex("Q妹");}System.out.println("领养成功!");pet1.print();while(pet1.getLove() != 0){System.out.println("你想和宠物做点什么?(1.吃! 2.玩!)");chose = sc.nextInt();if(chose == 1){pet1.eat();}else{pet1.play();}}System.out.println("游戏结束!");sc.close();}}
1 0
- Java小程序——petshop
- java小程序——简单计算器
- java 学习 ——计算器小程序
- Java小程序——汽车租赁
- Java程序设计——窗口小程序
- java小程序——人机猜拳
- java小程序——检测密码
- java小程序——豆机
- java小程序——八皇后
- Java拓展——随机数小程序
- java基础—applet小程序
- JAVA小程序—学生点名系统
- 小白Java笔记——第一个Java程序
- java学习——小程序——计算器
- 抽奖小程序——java中的多线程
- Java任选课学习——Building小程序
- Java——猜数字游戏小程序
- Java程序小技巧——字符串反转
- Java 产生 UUID / GUID
- mysql的主从配置
- yarn分布式(HA)
- Oracle 10046事件详解
- 双路录像利用静态变量做多对象之间简单写互斥
- Java小程序——petshop
- 关于AM5728评估板
- PL/SQL 连接远程oracle
- 消息队列RabbitMQ的安装
- 关于build 还有hermes流程
- ClassUtil.java代码解读
- 交换机下接非vlan1的主机,实现主机与路由器的通信配置方法
- 类和对象的区别
- Makefile中 -I -L -l区别