
正文
java狗玩耍游戏代码,游戏狗JAVA
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA大神来帮忙啊,ECLIPSE编写一个小程序类似于电子宠物的。弄完截图过来也行,行了就加大悬赏,谢谢!
其实是很简单的程序,就是无限循环和条件表达式的运用而已。代码如下:
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;
}
}
部分截图如下
相关问答
Q1: 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) {
this.nickname = nickname;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Q2: 用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()+"!");
}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()+"!");
}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) {
}
}
Q3: 求一个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("狗会算数");
}
}
如果有帮助到你,请点击采纳
Q4: JAVA代码主人喂宠物吃东西狗只吃骨头猫只吃鱼求代码
class 动物{
public boolean 吃(食物 sw){
}
}
class 狗 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 骨头) return true;
else return false;
}
}
class 猫 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 鱼) return true;
else return false;
}
}
你还需要建立一个 食物 类。







