您的位置:首页 > 编程语言 > Java开发

Java第十次作业

2017-11-20 00:00 232 查看
猎人类:

package org.jsoft.Game;

import java.lang.Math;
import java.util.Random;

public class Hunter {
/*猎人的属性:包括名字,生命值,武器,状态
*/
private String name;
private int life;
private String weapon;
private boolean isLive=true;
/*对应的set和get方法*/
int initialLife;
public Hunter(String name,int life,String weapon){
this.name=name;
this.life=life;
this.weapon=weapon;
initialLife=life;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
//猎人的方法,受伤,死亡,显示状态
/*
* 产生随机数取余数,决定僵尸状态
*/
public void fight(Monster monster){
Random rand = new Random();
int i = rand.nextInt();
i = rand.nextInt(100);
while(this.isLive==true){
if(i % 3 == 0){
System.out.println(name+"使用"+weapon+"的攻击命中!一击致命!");
monster.setLife(0);
break;
}
else if(i % 3 == 1){
System.out.println(name+"使用"+weapon+"的攻击未命中!");
break;
}
else{
System.out.println(name+"使用"+weapon+"的攻击命中!");
monster.setLife(monster.getLife()/2);
break;
}
}
}
public void injured(){
System.out.println(name+"受伤了,剩余的生命值"+life);
}
public void dead(){
this.setLive(false);
System.out.println(name+"已死亡");
}
public void show(){
if(life<initialLife & life>0){
injured();
}
else if(life<=0){
dead();
}
else{
System.out.println(name+"未受伤");
}
}
}

僵尸类:

package org.jsoft.Game;

import java.util.Random;

public class Monster {
/*僵尸的属性包括类型,状态和生命值
*/
private String type;
private boolean isLive=true;
private int life;
int initialLife;
public Monster(String type,int life){
this.type=type;
this.life=life;
initialLife=life;
}
//对应的set和get的方法
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
/*
* 僵尸类的方法
*/
public void injured(){
System.out.println(type+"受伤了,剩余的生命值"+life);
}
public void kill(Hunter hunter){
Random rand = new Random();
int i = rand.nextInt(); //int范围类的随机数
i = rand.nextInt(100); //生成0-100以内的随机数
while(this.isLive==true){
if(i % 3 == 0){
System.out.println(type+"的攻击命中!一击致命!");
hunter.setLife(0);
break;
}
else if(i % 3 == 1){
System.out.println(type+"的攻击未命中!");
break;
}
else{
System.out.println(type+"的攻击命中!");
hunter.setLife(hunter.getLife()/2);
break;
}
}
}
public void dead(){
this.setLive(false);
System.out.println(type+"已死亡");
}
public void show(){
if(life<initialLife & life>0){
injured();
}
else if(life<=0){
dead();
}
else{
System.out.println(type+"未受伤");
}
}

}

测试主类:

package org.jsoft.Game;

public class TestGame {
public static void main(String[] args) {
/*
* 创建猎人和僵尸实例化对象,通过随机数来判定每一次猎人和僵尸的攻击手段是否奏效
*/
Hunter h=new Hunter("MuggleHunter",100,"猎枪");
Monster m=new Monster("BigMonster",1000);
while(h.isLive()==true & m.isLive()==true){ //判断猎人和僵尸的状态
h.fight(m); //猎人先攻击僵尸
m.show();
if(m.isLive()==true){ //判断僵尸的状态,若死亡则结束
m.kill(h);
h.show();
}
}
}

}

输出结果:











以上是第十次作业。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java