宠物游戏;
main
package com.ishangu;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
System.out.println("**********欢迎来到小店************");
System.out.println("**********亲输入领养的宠物************");
Store s=new Store();
s.Store();
}
}
Dog类
package com.ishangu;
import java.util.Scanner;
public class Dog {
int age = 3;
String name;
String sex;
int mood;
int power;
int health;
int value;
public Dog(String name, int age, int mood, int power, int health, int value) {
this.name = name;
this.age = age;
this.mood = mood;
this.power = power;
this.health = health;
this.value = value;
}
public Dog setSex(Dog dog) {
System.out.println("请输入性别");
Scanner sc = new Scanner(System.in);
String sex = sc.next();
if (sex.equals("公") || sex.equals("母")) {
dog.sex = sex;
} else {
System.out.println("性别输入错误");
setSex(dog);
}
return dog;
}
public void eat(Dog dog) {
if (power >= 100) {
System.out.println("体力已满无需喂食");
} else {
System.out.println("骨头(1)");
System.out.println("狗粮(2)");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x == 1) {
System.out.println("狗狗体力值+3\t\t\t\t ");
this.power = this.power + 3;
int er = (int) (Math.random() * 10);
if (er >= 0 && er <= 2) {
System.out.println("消化不良 : 健康值-2");
dog.health = dog.health - 2;
} else if (er == 9 || er == 8) {
System.out.println("食物中毒 : 健康值-10");
dog.health = dog.health - 10;
}
} else if (x == 2) {
System.out.println("狗狗体力值+5\t\t\t\t ");
dog.power = dog.power + 5;
int en = (int) (Math.random() * 10);
if (en == 0) {
System.out.println("消化不良 : 健康值-2");
dog.health = dog.health - 2;
} else if (en == 9) {
System.out.println("食物中毒 : 健康值-10");
dog.health = dog.health - 10;
}
} else {
System.out.println("输入错误系统自动返回");
}
}
menu(dog);
}
public void play(Dog dog) {
System.out.println("狗狗接飞盘(1)");
System.out.println("狗狗玩皮球(2)");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x == 1) {
if (dog.power < 3) {
System.out.println("体力不足");
} else {
System.out.println("狗狗体力值-3\t 与主人亲密值+2");
dog.power = dog.power - 3;
dog.value = dog.value + 2;
}
} else if (x == 2) {
if (dog.power < 5) {
System.out.println("体力不足");
} else {
System.out.println("狗狗体力值-5\t 与主人亲密值+3");
dog.power = dog.power - 5;
dog.value = dog.value + 3;
}
} else {
System.out.println("输入错误系统自动返回");
}
menu(dog);
}
public String toString() {
return "姓名:" + name + "\t 性别:" + sex + "\t 年龄:" + "\n" + "心情:" + mood + "\t 体力:" + power + "\t健康:" + health
+ "\t 亲密值:" + value;
}
public void hospital(Dog dog) {
System.out.println("来到医院,宠物健康值+10");
dog.health = dog.health + 10;
if (dog.health > 100) {
dog.health = 100;
}
}
public void menu(Dog dog) {
System.out.println("1:玩耍\t2:喂食\t3:看病\t4:查看宠物属性\t5:退出");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x == 1) {
dog.play(dog);
} else if (x == 2) {
dog.eat(dog);
} else if (x == 3) {
if (dog.health > 100) {
System.out.println("健康值过高无需去医院");
} else if (dog.health <= 100) {
dog.hospital(dog);
}
menu(dog);
} else if (x == 4) {
System.out.println(dog);
menu(dog);
} else if (x == 5) {
Store.Store();
} else {
menu(dog);
}
}
}
Qq类
package com.ishangu;
import java.util.Scanner;
public class Qq {
int age=3;
String name;
String sex;
int mood=50;
int power=50;
int health=50;
int value=50;
public void Dog(String name){
this.name=name;
}
public void setSex(){
System.out.println("请输入性别");
Scanner sc = new Scanner(System.in);
String sex = sc.next();
if(sex.equals("公")||sex.equals("母")){
this.sex=sex;
}else{
System.out.println("性别输入错误");
setSex();
}
}
public void eat(){
System.out.println("小鱼干(1)");
System.out.println("饼干(2)");
Scanner sc =new Scanner(System.in);
int x=sc.nextInt();
if(x==1){
System.out.println("qq体力值+3\t\t\t\t ");
this.power=this.power+3;
int er=(int)(Math.random()*10);
if (er==0){
System.out.println("消化不良 : 健康值-2");
this.health=this.health-2;
}else if(er==9){
System.out.println("食物中毒 : 健康值-10");
this.health=this.health-10;
}
else if(x==2){
System.out.println("qq体力值+5\t\t\t\t ");
this.power=this.power+5;
int en=(int)(Math.random()*10);
if (er==0){
System.out.println("消化不良 : 健康值-2");
this.health=this.health-2;
}else if(er==9){
System.out.println("食物中毒 : 健康值-10");
this.health=this.health-10;
}
}else{
System.out.println("输入错误系统自动返回");
}
menu();
}
}
public void play(){
System.out.println("qq接转圈(1)");
System.out.println("qq玩扇子(2)");
Scanner sc =new Scanner(System.in);
int x=sc.nextInt();
if(x==1){
System.out.println("qq体力值-3\t 与主人亲密值+2");
this.power=this.power-3;
this.value=this.value+2;
}else if(x==2){
System.out.println("qq体力值-5\t 与主人亲密值+3");
this.power=this.power-5;
this.value=this.value+3;
}else{
System.out.println("输入错误系统自动返回");
}
menu();
}
public void attribute(){
System.out.println("姓名:"+name+"\t 性别:"+sex+"\t 年龄:"+
"\n"+"心情:"+mood+"\t 体力:"+power+"\t健康:"+health+"\t 亲密值:"+value);
menu();
}
public void hospital(){
System.out.println("来到医院,宠物健康值+10");
this.health= this.health+10;
}
public void menu(){
System.out.println("1:玩耍\t2:喂食\t3:看病\t4:查看宠物属性\t5:退出");
Scanner sc =new Scanner(System.in);
int x=sc.nextInt();
if (x==1){
this.play();
}else if(x==2){
this.eat();
}else if(x==3){
if(this.health>100){
System.out.println("健康值过高无需去医院");
}else if(this.health<=100){
this.hospital();
}
menu();
}else if(x==4){
this.attribute();
}else if(x==5){
Store.Store();
}
}
}
Store类
package com.ishangu;
import java.util.Scanner;
public class Store {
public static void Store() {
System.out.println("1:狗狗\t\t2:qq\t\t3:继续上次宠物\t4:注销");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x == 1) {
Dog dog = new Dog("小花");
dog.setSex();
dog.menu();
} else if (x == 2) {
Qq qq=new Qq();
qq.setSex();
qq.menu();
} else if (x == 3) {
} else if(x==4) {
System.out.println("注销成功............");
return;
}else{
Store();
}
}
}