面向对象入门(2)
来源:互联网 发布:淘宝复制软件 编辑:程序博客网 时间:2024/09/21 09:20
面向对象入门
练习:模拟奥特曼打小怪兽
package com.ck_study;/** * 奥特曼 * @author ck */public class Ultraman { private static final int MAX_MP = 100; private String name; private int hp; private int mp; /** * 构造器 * @param name 奥特曼的名字 */ public Ultraman(String name) { this.name = name; this.hp = 300; this.mp = MAX_MP; } /** * 攻击 * @param m攻击的目标 */ public void attack(Monster m) { int injury = (int) (Math.random() * 21 + 10); m.setHp(m.getHp() - injury); } /** * 究极必杀技 * @param m 小怪兽 */ public void hugeAttack(Monster m) { m.setHp(m.getHp() - 50); } /** * 魔法攻击 * @param ms 一群小怪兽 */ public void magicAttack(Monster[] ms) { mp -= 30; for (int i = 0; i < ms.length; i++) { if (mp >= 30) { ms[i].setHp(ms[i].getHp() - 20); } } } public int getHp() { return hp; } public int getMp() { return mp >= 30 ? mp : MAX_MP; } public void setMp(int mp) { this.mp = mp; } public void setHp(int hp) { this.hp = hp > 0 ? hp : 0; } @Override public String toString() { return "奥特曼" + name + "的血量为:" + hp; }}
package com.ck_study;/** * 小怪兽 * @author ck */public class Monster { private String name; private int hp; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp = hp > 0 ? hp : 0; } /** * 构造器 * @param name 小怪兽的名字 */ public Monster(String name) { this.name = name; this.hp = 100; } /** * 攻击 * @param u攻击的目标(谁) */ public void attack(Ultraman u) { int injury = (int) (Math.random() * 5 + 10); u.setHp(u.getHp() - injury); } public boolean isDie(Monster[] ms) { return true; } @Override public String toString() { return "小怪兽" + name + "的血量为:" + hp; }}
package com.ck_study;public class Pk { public static boolean hasAliveMonster(Monster[] ms) { for (int i = 0; i < ms.length; i++) { if (ms[i].isDie(ms)) { return true; } } return false; } public static void main(String[] args) { Ultraman u = new Ultraman("ck"); Monster[] ms = { new Monster("xgs1"), new Monster("xgs2"), new Monster("xgs3"), new Monster("xgs4"), new Monster("xgs5") }; int round = 1; do { System.out.println("=====第" + round++ + "回合====="); System.out.println("小怪兽攻击奥特曼"); Monster m = null; do { int index = (int) (Math.random() * ms.length); m = ms[index]; } while (!m.isDie(ms)); m.attack(u); System.out.println(u); 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.attack(m); } else { if (u.getMp() >= 30) { System.out.println("奥特曼使用魔法攻击"); for (int i = 0; i < ms.length; i++) { u.magicAttack(ms); } } else { System.out.println("奥特曼使用魔法攻击失败"); } } } for (Monster temp : ms) { System.out.println(temp); } } while (u.getHp() > 0 && hasAliveMonster(ms)); if (u.getHp() > 0) { System.out.println("奥特曼胜利!!!"); } else { System.out.println("小怪兽胜利!!!"); } }}
0 0
- 面向对象入门(2)
- 【黑马程序员】面向对象入门(2)
- 【JavaSE笔记】面向对象(一)_面向对象入门
- Java学习2 面向对象程序设计入门
- Java知识(008)--面向对象入门
- JavaWeb入门(二) 面向对象篇
- JavaScript 实现面向对象(入门)
- Java实现面向对象编程(入门)
- Java面向对象入门讲解(深入浅出)
- java入门心得(面向对象概念)
- Java面向对象入门
- 面向对象入门
- 面向对象入门知识
- 面向对象入门
- 面向对象-入门
- Java面向对象入门
- js面向对象入门
- PYTHON面向对象入门
- 错误代码: 1054 Unknown column 'course' in 'field list'
- e5-4 uva165
- 4.22
- Android 之Java基础---一个列表框引发的血案暨java装箱拆箱的实际应用
- IDEA 实用教程
- 面向对象入门(2)
- 工作一年纪
- greenplum的扩容(1)-前期准备
- 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
- 使用Xamarin + C#开发应用 -- 使用sqlite做本地存储
- 作为计算机专业学生,最应该学习的课程前五位是什么?【知乎】
- C++标准转换运算符static_cast
- 关于解析json数组中的json数组
- Mac下PHP环境搭建