Java小程序——petshop
2016-09-07 11:50
288 查看
《Java小程序——petshop》
1.描述:刚学Java写的小程序,用到类的封装,使用。
2.工具:eclipse.exe
3.功能:吃,玩。
1.描述:刚学Java写的小程序,用到类的封装,使用。
2.工具:eclipse.exe
3.功能:吃,玩。
/***************************************************** Author:Ivan Version:0.1 Date: File name:Pet.java Description:类 *****************************************************/ 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.java Description:main *****************************************************/ import java.util.Scanner; public class Shop { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Pet 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(); } }
相关文章推荐
- Java程序性能和速度优化实例
- 利用java编写网络通信程序
- 为已有的Java图形程序增加储存WMF矢量文件功能
- JML起步---使用JML 改进你的Java程序(1)
- Java 中怎样在程序中设置代理服务器
- JML起步---使用JML 改进你的Java程序(2)
- 在Java程序,如何实现自适应位置的快捷菜单
- C Java PHP Perl Python 的程序代码美化工具
- 如何有效的保护JAVA程序
- 编写跨平台Java程序注意事项
- JBuilder下调试java程序最简单的例子
- 从java程序中调用其它程序
- 提升JAVA程序的性能
- 如何设计出具有GUI特色的“纯”JAVA程序
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- Thinking in java 中文版 程序源代码
- Java 程序编码规范
- NakedObjects框架: 最快最省构建漂亮合理的Java程序
- JML起步---使用JML 改进你的Java程序(4)
- Java程序的国际化和本地化介绍