Java第十次作业

猎人类:

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();
        }
        }
    }
    
}
 

输出结果:

192346_WXBb_3715361.png

192356_DuJa_3715361.png

192406_A7JP_3715361.png

192421_Vo73_3715361.png

192453_wzbv_3715361.png

以上是第十次作业。

 

 

 

转载于:https://my.oschina.net/u/3715361/blog/1576304

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值