其实是很简单的程序,就是无限循环和条件表达式的运用而已.代码如下:
import?java.util.Scanner;
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;
//?输入天数
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;
部分截图如下
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)?{
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{
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?{
代码如下
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("狗会算数");
class 动物{
public boolean 吃(食物 sw){
class 狗 extends 动物{
if(sw.种类 == 骨头) return true;
else return false;
class 猫 extends 动物{
if(sw.种类 == 鱼) return true;
你还需要建立一个 食物 类.
以上就是土嘎嘎小编为大家整理的java狗玩耍游戏代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!