Java基础
面向对象练习题
1.双色球。
package com.lovoinfo;
/**
* 球(双色球摇奖用的数字球)
* @author zihao
*
*/
public class Ball {
private int number;//号码
private boolean used;//是否使用过
public boolean isUsed() {
return used;
}
/**
* used属性的访问器
* @param used
*/
public void setUsed(boolean used) {
this.used = used;
}
/**
* used属性的修改器
* @param number
*/
public Ball(int number) {
this.number = number;
}
/**
* number属性的访问器
* @return
*/
public int getNumber() {
return number;
}
/**
* 获得字符串形式的球的号码(补0)
* @return 字符串形式的号码
*/
public String getString(){
return number < 10? "0" + number : "" + number;
}
}
package com.lovoinfo;
import java.util.Arrays;
import java.util.Comparator;
/**
* 双色球摇号机
* @author yanghao
*
*/
public class LotteryMachine {
private Ball[] redBalls = new Ball[33];
private Ball[] blueBalls = new Ball[16];
/**
* 装入红色球和蓝色球
*/
public void load(){
for (int i = 0; i < redBalls.length; i++) {
redBalls[i] = new Ball(i + 1);
}
for (int i = 0; i < blueBalls.length; i++) {
blueBalls[i] = new Ball(i + 1);
}
}
/**
* 摇出一个蓝色球
* @return 球的对象
*/
public Ball getBlueBall(){
int randomIndex = (int) (Math.random() * blueBalls.length);
return blueBalls[randomIndex];
}
/**
* 摇出六个红色球
* @return 装红色球的数组
*/
public Ball[] getRedBalls(){
Ball[] balls = new Ball[6];
for (int i = 0; i < balls.length; i++) {
Ball tempBall = null;
do{
int randomIndex = (int) (Math.random() * redBalls.length);
tempBall = redBalls[randomIndex];
} while(tempBall.isUsed());
balls[i] = tempBall;
tempBall.setUsed(true);
}
Arrays.sort(balls, new Comparator<Ball>() {
@Override
public int compare(Ball o1, Ball o2) {
return o1.getNumber() - o2.getNumber();
}
});
return balls;
}
/**
* 生成随机号码
* @return 随机号码的字符串
*/
public String generateRedomNumber(){
this.load();
Ball[] rBalls = this.getRedBalls();
Ball bBall = this.getBlueBall();
String str = " ";
for (int i = 0; i < rBalls.length; i++) {
str += rBalls[i].getNumber() + " ";
}
str += "|";
str += bBall.getNumber();
return str;
}
}
2.奥特曼打小怪兽
package com.lovoinfo;
/**
* 奥特曼
* @author yanghao
*
*/
public class Utm {
private static final int MAX_MAGIC = 100;
private String name;//名字
private int hp;//生命值
private int mp;//魔法值
/**
* 构造器
* @param name 名字
*/
public Utm(String name){
this.name = name;
this.hp = 300;
this.mp = MAX_MAGIC;
}
/**
* 攻击
* @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++) {
Monster m = ms[i];
if (m.isAlive()) {
m.setHp(m.getHp() - 20);
}
}
}
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 < MAX_MAGIC? mp : MAX_MAGIC;
}
@Override
public String toString() {
return "奥特曼" + name + "生命值" + hp;
}
}
package com.lovoinfo;
/**
* 小怪兽
* @author yanghao
*
*/
public class Monster {
private String name; //名字
private int hp; //生命值
/**
* 构造器
* @param name 名字
*/
public Monster(String name){
this.name = name;
this.hp = 100;
}
/**
* 攻击
* @param u 奥特曼
*/
public void attack(Utm u){
int injury = (int) (Math.random() * 11 + 5);
u.setHp(getHp() - injury);
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp > 0? hp : 0;
}
/**
* 判断死活
* @return 活着返回ture 死了返回false
*/
public boolean isAlive() {
return hp > 0;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "小怪兽" + name + "生命值" +hp;
}
}
package com.lovoinfo;
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) throws Exception {
Utm u = new Utm("灰太狼");
Monster[] ms = {
new Monster("喜羊羊"),
new Monster("美羊羊"),
new Monster("懒羊羊"),
new Monster("沸羊羊"),
new Monster("小灰灰")
};
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());
m.attack(u);
System.out.println(m.getName() + "小怪兽攻击奥特曼");
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.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("小怪兽胜利!!!");
}
}
}