Java day 05丶06

Java 第八天

package day0506;
/**

  • 奥特曼
  • @author Administrator
  • */
    public class Ultraman {
    private String name;
    private int hp;
    private int mp;
    /**
    • 构造器
    • @param name
      */
      public Ultraman(String name){
      this.name = name; // 名字
      this.hp = 500; // 生命值
      this.mp = 100; // 魔法值
      }
      /**
    • 普通攻击
    • @param m
      /
      public void attack(Monster m){
      int injury = (int) (Math.random()
      121+90);
      m.setHp(m.getHp()-injury);
      }
      /**
    • 致命一击
    • @param m
      */
      public void hugeAttack(Monster m) {
      m.setHp(m.getHp()-398);
    }
    /**
    • 魔法攻击
    • @param ms
      */
      public void magicAttack(Monster[] ms){
      for (int i = 0; i < ms.length; i++) {
      Monster m = ms[i];
      if (m.isAlive()) {
      m.setHp(m.getHp()-198);
      }else {
      m.setHp(0);
      }
      }
      }
      public int getHp() {
      return hp;
      }
      public void setHp(int hp) {
      this.hp = hp > 0? hp : 0;
      }
    public int getMp() {
    return mp;
    }
    public void setMp(int mp) {
    this.mp = mp;
    }
    @Override
    public String toString() {
    return name+"的生命值"+hp;
    }

}
———————————————————————————————————————————————————————————————————————————————
package day0506;
/**

  • 小怪兽
  • @author Administrator
  • */
    public class Monster {
    private String name;
    private int hp;

/**

  • 构造器
  • @param name
    */
    public Monster(String name){
    this.name = name;
    this.hp = 1000;
    }

    /**
    • 普通攻击
    • @param m
      /
      public void attack(Ultraman u){
      int injury = (int) (Math.random()
      31+10);
      u.setHp(u.getHp()-injury);
      }

    public int getHp() { //修改器
    return hp;
    }

    public void setHp(int hp) { //访问器
    this.hp = hp > 0? hp : 0;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    @Override
    public String toString() {
    return name+"的生命值"+hp;
    }
    public boolean isAlive(){
    return this.hp>0;
    }
    }
    ———————————————————————————————————————————————————————————————————————————————
    package day0506;
    /**
  • 一群小怪兽群殴奥特曼
  • @author Administrator
  • */
    public class Pk {
    /**
    • 判断是否至少有一只小怪兽是活着的
    • @param ms小怪兽的数组
    • @return 有活着的小怪兽返回true否则返回false
      */
      public static boolean hasAliveMonster(Monster[] ms){
      for (int i = 0; i < ms.length; i++) {
      if (ms[i].isAlive()) {
      return true;
      }
      }
      return false;
      }
      public static void main(String[] args) {
      Ultraman u = new Ultraman("英雄V");
      Monster[] ms = {
      new Monster("僵尸A"),
      new Monster("僵尸B"),
      new Monster("僵尸C"),
      new Monster("僵尸D"),
      new Monster("僵尸E")
      };

      int round = 1;
      do {
      System.out.println("=========第" + round++ + "回合=========");
      Monster m = null;
      do {
      int mIndex = (int) (Math.random()*ms.length);
      m = ms[mIndex];
      } while (!m.isAlive());
      System.out.println(m);
      System.out.println(u);
      m.attack(u);
      System.out.println(m.getName()+"小怪兽攻击奥特曼");
      if (u.getHp() > 0) {
      u.setMp(u.getMp()+5);
      double rate = Math.random();
      if (rate <= 0.8) {
      System.out.println("奥特曼攻击小怪兽");
      u.attack(m);
      }
      else if (rate<=0.9) {
      System.out.println("奥特曼使用致命一击");
      u.hugeAttack(m);
      }
      else {
      if (u.getMp()>=30) {
      System.out.println("奥特曼使用魔法攻击");
      u.magicAttack(ms);
      }
      else {
      System.out.println("奥特曼使用魔法失败");
      }
      }
      for (Monster tempMonster : ms) {
      System.out.println(tempMonster);
      }
      }
      } while (u.getHp() > 0 && hasAliveMonster(ms));

      if (u.getHp() > 0) {
      System.out.println("奥特曼胜利!!");
      } else {
      System.out.println("小怪兽胜利!!");
      }
      }

}

转载于:https://www.cnblogs.com/Z356571403/p/4483563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值