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();
}
}
}
}
输出结果:
![](https://static.oschina.net/uploads/space/2017/1120/192346_WXBb_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192356_DuJa_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192406_A7JP_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192421_Vo73_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192453_wzbv_3715361.png)
以上是第十次作业。
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();
}
}
}
}
输出结果:
![](https://static.oschina.net/uploads/space/2017/1120/192346_WXBb_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192356_DuJa_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192406_A7JP_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192421_Vo73_3715361.png)
![](https://static.oschina.net/uploads/space/2017/1120/192453_wzbv_3715361.png)
以上是第十次作业。
相关文章推荐
- 吉软_Java57_第十次作业
- 吉软_java57_第十次作业
- JAVA 第十次作业
- java-java57-第十次作业
- Java第十次作业--多线程
- java第十次作业
- java第十次作业:oop的第6张图片到第11张图片
- [JAVA初学] 第一次作业 hello.java
- C语言第十次博客作业--结构体
- 吉软-Java57-第一次作业
- 第六次Java作业
- 第六次java作业
- 从批处理文件中抽取作业号以及所有调用的JAVA CLASS名
- 第十五周Java作业-1线程同步机制
- 黑马作业(d33)java反射训练
- [作业]Java基础知道简要
- 大三学长带我学习JAVA.作业7 利用for循环打印 9*9 表
- 2017《JAVA技术》预备作业-计科1502-19-何俏依
- java作业 流水线
- java作业