java养宠物游戏代码 一款抓宠物合成的java游戏

Java优化宠物系统 求代码 求注解四个类:Pet Dog Penguin PetTest(测试类)
1、Pet类:
/**
* 宠物类
* Created by LuHuan on 2017/7/27.
*/
public class Pet {
String name = "null";
int health = 100;//健康值
int love = 0;//亲密度
//打印输出宠物信息
public void print() {
System.out.print("java养宠物游戏代码我的名字叫"name"java养宠物游戏代码,我的健康值是:"health",我和主人的亲密程度是"love".");
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getLove() {
return love;
}
}
2、Dog类:
/**
* 狗狗类
* Created by LuHuan on 2017/7/27.
*/
public class Dog extends Pet {
String strain = "拉布拉多犬";//品种
public String getStrain() {
return strain;
}
//重写宠物的自白方法
@Override
public void print() {
super.print();
System.out.println("我是一只"strain);
}
}
3、Penguin类:
/**
*企鹅类
* Created by LuHuan on 2017/7/27.
*/
public class Penguin extends Pet {
String sex = "Q仔";//性别
public String getSex() {
return sex;
}
//重写宠物的自白方法
@Override
public void print() {
super.print();
System.out.println("我的性别是"sex);
}
}
4、PetTest测试类:
/**
* 测试类
* Created by LuHuan on 2017/7/27.
*/
public class PetTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "欧欧";
Penguin pgn = new Penguin();
pgn.name = "楠楠";
System.out.println("宠物的自白:");
dog.print();
System.out.println("宠物的自白:");
pgn.print();
}
}
5、输出:
宠物的自白:
我的名字叫欧欧 , 我的健康值是:100,我和主人的亲密程度是0.我是一只拉布拉多犬
宠物的自白:
我的名字叫楠楠 , 我的健康值是:100,我和主人的亲密程度是0.我的性别是Q仔
JAVA大神来帮忙?。珽CLIPSE编写一个小程序类似于电子宠物的 。弄完截图过来也行 , 行了就加大悬赏,谢谢!其实是很简单的程序,就是无限循环和条件表达式的运用而已 。代码如下:
import java.util.Scanner;
public class ElectronicDog {
private static double timeLeft = 0; // 剩余时间
private static int happiness = 1; // 幸福值(初始为1)
private static int energy = 2; // 能量值(初始为2)
private static Scanner sc;
private static double sleepTimeLeft = 10; // 睡眠时间间隔,超过则小狗离开
private static double feedTimeLeft = 0; // 饲养时间间隔,小于间隔则无效
public static void main(String[] args) {
sc = new Scanner(System.in);
int days = 0;
// 输入天数
while(days = 0) {
System.out.print("Play time (day(s)): ");
days = sc.nextInt();
if(days = 0) {
System.out.println("Please input a valid time.");
}
}
// 剩余时间就是天数乘以24小时
timeLeft = days * 24;
while(timeLeft0) { // 如果剩余时间小于0则游戏结束
showState();
if(happiness = 0) { // 幸福值小于0时结束
System.out.println("The dog is out of happiness and has left.");
break;
}
if(energy = 0) { // 能量值小于0时结束
System.out.println("The dog is out of energy and has died.");
break;
}
if(sleepTimeLeft = 0) { // 睡眠时间超过时结束
System.out.println("The dog does not sleep within 10 hours and has left.");
break;
}
Command ops = getCommand();
if(ops == Command.Sleep) { // 如果是让宠物睡觉则睡眠时间重置
sleepTimeLeft = 10;
} else { // 如果不是让宠物睡觉则睡眠时间减去任务消耗时间
if(ops == Command.Feed) {
if(feedTimeLeft0) { // 如果太过频繁喂养,则本次命令无效
continue;
} else { // 如果喂养有效,则喂养时间重置
feedTimeLeft = 3;
}
}
sleepTimeLeft -= ops.timeSpent;
}
feedTimeLeft -= ops.timeSpent;
happiness= ops.happinessGained;
energy= ops.energyConsumed;
timeLeft -= ops.timeSpent;
}
if(timeLeft = 0) { // 如果是正常游戏时间结束才能看到这一句
System.out.println("Play time is over.");
}
}
/**
* 显示宠物状态
*/
private static void showState() {
System.out.println("--------------------------------");
System.out.println("Time left: "getTime());
System.out.println("Dog's happiness: "happiness);
System.out.println("Dog's energy: "energy);
}
/**
* 命令宠物行为
* @return 返回操作明细
*/
private static Command getCommand() {
System.out.println("1. Walk\r\n2. Feed\r\n3. Sleep\r\n4. Play");
int choice = 0;
while(choice = 0 || choice4) {
System.out.print("Choice: ");
if(sc.hasNextInt()) {
choice = sc.nextInt();
}
}
Command cmd = Command.values()[choice - 1];
return cmd;
}
private static String getTime() {
int hour = (int) Math.floor(timeLeft);
double min = timeLeft - hour;
return String.format("d : d", hour, (int) (min * 60));
}
}
/**
* 对小狗发出的命令
*/
enum Command {
Walk(1, 3, -2), Feed(0.5f, 1, 5), Sleep(8, -8, -4), Play(2, 2, -1);
public float timeSpent;
public int happinessGained;
public int energyConsumed;
private Command(float timeSpent, int happinessGained, int energyConsumed) {
this.timeSpent = timeSpent;
this.happinessGained = happinessGained;
this.energyConsumed = energyConsumed;
}
}
部分截图如下
用Java程序完成以下场景(用继承多态):有一个主人(Master类),他养了两只宠物(Pet类)public class Run {
public static void main(String[] args) {
Master master = new Master();
master.feedDog("鸡骨头");
master.feedCat("鸡骨头");
}
}
class Master {
private Pet mPet;
private Food mFood;
public void feedCat(String food) {
mPet = new Cat();
mFood = new Food(food);
mPet.eat(mFood);
}
public void feedDog(String food) {
mPet = new Dog();
mFood = new Food(food);
mPet.eat(mFood);
}
}
class Dog extends Pet{
@Override
public void eat(Food food) {
System.out.println("正在喂小狗吃" food.getFood());
if (food.getFood().matches(Food.BONE)) {
System.out.println("小狗正在吃" food.getFood() "java养宠物游戏代码!");
}else {
System.out.println("但是小狗不喜欢吃" food.getFood() "!");
}
}
}
class Cat extends Pet{
@Override
public void eat(Food food) {
System.out.println("正在喂小猫吃" food.getFood());
if (food.getFood().matches(Food.FISH)) {
System.out.println("小猫正在吃" food.getFood() "java养宠物游戏代码!");
}else {
System.out.println("但是小猫不喜欢吃" food.getFood() "!");
}
}
}
class Food {
public final static String BONE = ".*骨.*";
public final static String FISH = ".*鱼.*";
private String food;
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public Food(String food) {
this.food = food;
}
}
class Pet {
public void eat(Food food) {
}
}
【java养宠物游戏代码 一款抓宠物合成的java游戏】关于java养宠物游戏代码和一款抓宠物合成的java游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读