JAVA程序设计(09)-----面对对象设计初级应用 奥特曼打小怪兽

1.一个奥特曼固定属性 4个小怪兽随机名字 随机属性 

奥特曼有几率发动暴风雪技能 对所有小怪兽造成 50%~60%冰霜伤害 无视防御力

普通攻击伤害为 攻击力*80~120% - 对方防御力*60%~100%; 然后开始打……

package com.lovo;

/**
 * 类 : 奥特曼 和 小怪兽
 * 
 * @author Abe
 */

public class Ultraman {
	/**
	 * 属性:名字 生命值上限 攻击力 防御力 伤害 死活
	 */
	private String name;
	private int hp;
	private int maxhp;
	private int attack;
	private int defence;
	private int dam;
	private boolean alive = true;

	/**
	 * 构造器
	 */
	public Ultraman(String name, int hp, int maxhp, int attack, int defence) {
		this.name = "奥特曼" + name;
		this.hp = hp;
		this.maxhp = maxhp;
		this.attack = attack;
		this.defence = defence;
	}

	public Ultraman() {
		String str = "小怪兽" + (char) (Math.random() * 26 + 65);
		for (int n = 1; n <= 3; n++) {
			str += (char) (Math.random() * 26 + 97);
			this.name = str;
		}
		this.hp = this.maxhp = (int) (Math.random() * 351 + 350);
		this.attack = (int) (Math.random() * 61 + 60);
		this.defence = (int) (Math.random() * 31 + 50);
	}

	/**
	 * 随机重置名字 生命 攻击 防御
	 */
	public void resect() {
		String str = "重生的小怪兽" + (char) (Math.random() * 26 + 65);
		for (int n = 1; n <= 3; n++) {
			str += (char) (Math.random() * 26 + 97);
			this.name = str;
		}
		this.hp = this.maxhp = (int) (Math.random() * 351 + 350);
		this.attack = (int) (Math.random() * 61 + 60);
		this.defence = (int) (Math.random() * 31 + 50);
	}

	/**
	 * 动作:输出属性 名字 现有生命/生命上限 攻击 防御
	 */
	public String info() {
		return name + "  生命值为:" + hp + "/" + maxhp + " \t攻击力:" + attack
				+ "\t防御力:" + defence + "\t状态:" + (alive ? "活着" : "挂了");
	}

	/**
	 * 动作:普通攻击
	 */
	public String attack(Ultraman other) {
		this.dam = (int) (this.attack * (Math.random() * 41 + 80) / 100)
				- (int) (other.defence * (Math.random() * 41 + 60) / 100);
		if (dam <= 0) {
			dam = 1;
			other.hp -= dam;
		} else if (dam < other.hp) {
			other.hp -= dam;
		} else if (dam >= other.hp) {
			other.hp = 0;
			other.alive = false;
			return name() + "攻击了" + other.name() + "造成" + this.dam() + "点伤害,"
					+ other.name() + "挂掉了O(∩_∩)O哈哈~";
		}
		return name() + "攻击了" + other.name() + "造成" + this.dam() + "点伤害";
	}

	/**
	 * 动作:群体攻击
	 */
	public String magic(Ultraman other) {
		this.dam = this.attack * (int) (Math.random() * 11 + 50) / 100;
		if (dam < other.hp) {
			other.hp -= dam;
		} else if (dam >= other.hp) {
			other.hp = 0;
			other.alive = false;
			return other.name() + "挂掉了O(∩_∩)O哈哈~\n";
		}
		return "";
	}

	/**
	 * 动作: 获得 姓名 现有血量 最大血量 攻击 防御 设置现有血量
	 */
	public String name() {
		return name;
	}

	public int hp() {
		return hp;
	}

	public int maxHp() {
		return maxhp;
	}

	public int attack() {
		return attack;
	}

	public int defence() {
		return defence;
	}

	public int dam() {
		return dam;
	}

	public boolean alive() {
		return alive;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

}

然后是运行程序,开始搞了半天想把数据库加进去,结果发现必须频繁在 类与外面的数据库 间调用…… 后来干脆就算了,下次再试试做个就用数据的……

package com.lovo;
/**
 * 奥特曼 开始打 小怪兽们
 * @author Abe
 */
public class Ultramantest {

	public static void main(String[] args) {
		Ultraman mon1 = new Ultraman();
		Ultraman mon2 = new Ultraman();
		Ultraman mon3 = new Ultraman();
		Ultraman mon4 = new Ultraman();
		Ultraman man = new Ultraman("甘道夫", 1200, 1200, 120, 80);
		int[] hp = new int[4];

		System.out.println(mon1.info());
		System.out.println(mon2.info());
		System.out.println(mon3.info());
		System.out.println(mon4.info());
		System.out.println(man.info());
		for (int i = 1; man.alive()
				&& (mon1.alive() || mon2.alive() || mon3.alive() || mon4
						.alive()); i++) {
			
			System.out.println("----------第" + i++ + "轮----------");

			if (Math.random() * 100 >= 70) {
				System.out.println(man.name() + "释放了暴风雪,对所有小怪兽造成了"
						+ man.attack() * 55 / 100 + "点左右的冰霜伤害!!");//随然是群攻,还是只能一个一个打……还要判断死活
				if (mon1.alive()) {
					System.out.print(man.magic(mon1));
				}
				if (mon2.alive()) {
					System.out.print(man.magic(mon2));
				}
				if (mon3.alive()) {
					System.out.print(man.magic(mon3));
				}
				if (mon4.alive()) {
					System.out.print(man.magic(mon4));
				}
			}
			if (mon1.alive()) {<span style="white-space:pre">					</span>//奥特曼普攻
				System.out.println(man.attack(mon1));
			} else if (mon2.alive()) {
				System.out.println(man.attack(mon2));
			} else if (mon3.alive()) {
				System.out.println(man.attack(mon3));
			} else if (mon4.alive()) {
				System.out.println(man.attack(mon4));
			}
			if (mon1.alive()) {<span style="white-space:pre">					</span>//小怪兽们的反击
				System.out.println(mon1.attack(man));
			}
			if (mon2.alive() && man.alive()) {
				System.out.println(mon2.attack(man));
			}
			if (mon3.alive() && man.alive()) {
				System.out.println(mon3.attack(man));
			}
			if (mon4.alive() && man.alive()) {
				System.out.println(mon4.attack(man));
			}
			System.out.println(mon1.info());<span style="white-space:pre">			</span>//一轮过后状态显示
			System.out.println(mon2.info());
			System.out.println(mon3.info());
			System.out.println(mon4.info());
			System.out.println(man.info());
		}
		if (man.alive()) {
			System.out.println(man.name() + "取得了最后的胜利~ 保卫了地球~~");
		} else {
			System.out.println("地球被小怪兽们统治了…………");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值