网站首页 > 文章中心 > 其它

java狗玩耍游戏代码

作者:小编 更新时间:2023-08-08 14:30:03 浏览量:470人看过

JAVA大神来帮忙啊,ECLIPSE编写一个小程序类似于电子宠物的.弄完截图过来也行,行了就加大悬赏,谢谢!

其实是很简单的程序,就是无限循环和条件表达式的运用而已.代码如下:

import?java.util.Scanner;

java狗玩耍游戏代码-图1

public?class?ElectronicDog?{

private?static?double?timeLeft?=?0;?//?剩余时间

private?static?int?happiness?=?1;?//?幸福值(初始为1)

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;

java狗玩耍游戏代码-图2

//?输入天数

while(days?=?0)?{

System.out.print("Play?time?(day(s)):?");

days?=?sc.nextInt();

if(days?=?0)?{

System.out.println("Please?input?a?valid?time.");

}

while(timeLeft?0)?{?//?如果剩余时间小于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.");

if(sleepTimeLeft?=?0)?{?//?睡眠时间超过时结束

System.out.println("The?dog?does?not?sleep?within?10?hours?and?has?left.");

Command?ops?=?getCommand();

if(ops?==?Command.Sleep)?{?//?如果是让宠物睡觉则睡眠时间重置

sleepTimeLeft?=?10;

}?else?{?//?如果不是让宠物睡觉则睡眠时间减去任务消耗时间

if(ops?==?Command.Feed)?{

if(feedTimeLeft?0)?{?//?如果太过频繁喂养,则本次命令无效

continue;

}?else?{?//?如果喂养有效,则喂养时间重置

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);

*?命令宠物行为

private?static?Command?getCommand()?{

int?choice?=?0;

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;

*?对小狗发出的命令

enum?Command?{

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 狗狗类?

public?class?Dog?{

*?昵称

private?String?nickname;

*?品种

private?String?type;

*?颜色

private?String?color;

public?void?selfIntroduction(){

System.out.println("Dog{"?+

"昵称='"?+?nickname?+?'\''?+

",?品种='"?+?type?+?'\''?+

",?颜色='"?+?color?+?'\''?+

'}');

public?Integer?speed(){

//不清楚具体需求?可额外设置个属性?返回该属性?或在该方法中写自己的计算公式

return?1;

public?Dog()?{

public?Dog(String?nickname,?String?type,?String?color)?{

this.nickname?=?nickname;

this.type?=?type;

this.color?=?color;

public?String?getNickname()?{

return?nickname;

public?void?setNickname(String?nickname)?{

public?String?getType()?{

return?type;

public?void?setType(String?type)?{

public?String?getColor()?{

return?color;

public?void?setColor(String?color)?{

用Java程序完成以下场景(用继承多态):有一个主人(Master类),他养了两只宠物(Pet类)

public?class?Run?{

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();

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()+"!");

}else?{

System.out.println("但是小狗不喜欢吃"+food.getFood()+"!");

class?Cat?extends?Pet{

java狗玩耍游戏代码-图3

System.out.println("正在喂小猫吃"+food.getFood());

if?(food.getFood().matches(Food.FISH))?{

System.out.println("小猫正在吃"+food.getFood()+"!");

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)?{

class?Pet?{

求一个JAVA∶猫狗案例代码

代码如下

abstract class Dongwu {

void chi(){

System.out.println("吃");

interface Tiao{

void tiao();

interface Suan{

void suan();

class Mao extends Dongwu implements Tiao{

public void tiao() {

System.out.println("猫会跳高");

class Gou extends Dongwu implements Suan{

public void suan() {

System.out.println("狗会算数");

JAVA代码主人喂宠物吃东西狗只吃骨头猫只吃鱼求代码

class 动物{

public boolean 吃(食物 sw){

class 狗 extends 动物{

if(sw.种类 == 骨头) return true;

else return false;

class 猫 extends 动物{

if(sw.种类 == 鱼) return true;

你还需要建立一个 食物 类.

以上就是土嘎嘎小编为大家整理的java狗玩耍游戏代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章