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

 我来答
duoloniori
2014-12-17 · TA获得超过456个赞
知道小有建树答主
回答量:181
采纳率:0%
帮助的人:227万
展开全部

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

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(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.");
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(feedTimeLeft > 0) { // 如果太过频繁喂养,则本次命令无效
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 || choice > 4) {
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("%02d : %02d", 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;
}
}

部分截图如下

laobaitu0322
2014-12-17 · TA获得超过744个赞
知道小有建树答主
回答量:900
采纳率:33%
帮助的人:628万
展开全部
import java.util.Scanner;

public class Test{

public static void main(String[]args){
ElectronicDog dog = new ElectronicDog();
while(!dog.isOver()) {
dog.MenuInput();
}
}
}

class ElectronicDog {

int time, happiness, energy, lastsleep, lastfeed;
Scanner sc = new Scanner(System.in);

public ElectronicDog() {
System.out.print("Play time (day(s)): ");
int days = sc.nextInt();
time = days * 48;
happiness = 10;
energy = 10;
lastsleep = 0;
lastfeed = 6;
}

private void MenuHead() {
System.out.println("============================");
System.out.println("Time left: " + time/2 + ":" + (time%2==0?"00":"30"));
System.out.println("Dog's happiness: " + happiness);
System.out.println("Dog's energy: " + energy);
}

public void MenuInput() {
MenuHead();
System.out.println("1. Walk");
System.out.println("2. Feed");
System.out.println("3. Sleep");
System.out.println("4. Play");
System.out.print("Choice: ");
int cho = sc.nextInt();
switch(cho) {
case 1:
Walk();
break;
case 2:
Feed();
break;
case 3:
Sleep();
break;
case 4:
Play();
break;
default:
Error();
break;
}
}

public boolean isOver() {
return isTime() || isDead() || isLeave();
}

private void Walk() {
time -= 2;
happiness += 3;
energy -= 2;
lastsleep += 2;
lastfeed += 2;
}

private void Feed() {
if(lastfeed >= 6) {
time -= 1;
happiness += 1;
energy += 5;
lastsleep += 1;
lastfeed = 0;
} else {
System.out.println("The duration of feeding is 3 hours.");
}
}

private void Sleep() {
time -= 16;
happiness -= 8;
energy -= 4;
lastsleep = 0;
lastfeed += 16;
}

private void Play() {
time -= 4;
happiness += 2;
energy -= 1;
lastsleep = 4;
lastfeed += 4;
}

private void Error() {
System.out.println("Choice invalid! Please choose from 1~4.");
}

private boolean isTime() {
boolean ret = time <= 0;
if(ret) {
MenuHead();
System.out.println("You can adopt the dog forever.");
sc.close();
}
return ret;
}

private boolean isDead() {
boolean ret = energy <= 0;
if(ret) {
MenuHead();
System.out.println("Your dog is out of energy and has died.");
sc.close();
}
return ret;
}

private boolean isLeave() {
boolean ret = happiness <= 0 || lastsleep >= 20;
if(ret) {
MenuHead();
System.out.println("Your dog is out of happiness or sleep and has left.");
sc.close();
}
return ret;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式