您的位置:首页 > 移动开发 > 微信开发

Java小程序——petshop

2016-09-07 11:50 288 查看
《Java小程序——petshop》

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