Java学习第二周总结

2016-12-09  本文已影响0人  a04b20daaf33

面向对象

  1. 对象都有属性和行为
  2. 每一个对象都是第一无二的
  3. 对象都属于某个类
package com.baidu;

public class Ultraman {
    private String name;
    private int hp;         //Health Point
    private int mp;
    
    public Ultraman(String name, int hp, int mp) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp < 0 ? 0 : hp;
    }

    public int getMp() {
        return mp;
    }

    public void setMp(int mp) {
        this.mp = mp;
    }
    public void resumeMp(){
        mp += 4;
    }

    public String info(){
        return String.format("%s奥特曼 - 生命值 : %d, 魔法值 : %d", 
                name,hp,mp);
    }
    public void attack(Monster m){
        int injury = (int) (Math.random()  * 11  + 10);
        m.setHp(m.getHp() - injury);
    }
    
    public void hugeAttack(Monster m){
        if(mp >= 40){
            int injury = m.getHp() /4 * 3 > 50 ? m.getHp() / 4 * 3 : 50;
            m.setHp(m.getHp() - injury);
            mp -= 40;
        }
        
    }
    public void magicalAttack(Monster[] mArray){
        if(mp >= 15){
            for (Monster m: mArray) {
                if(m.getHp() > 0){
                int injury = (int) (Math.random() * 5  + 15);
                m.setHp(m.getHp() - injury);
                }
            }
                mp -= 15;
        }
    }
}

怪兽类

package com.baidu;

public class Monster {
    private String name;
    private int hp;
    
    public Monster(String name, int hp) {
        this.name = name;
        this.setHp(hp);
    }
    
    public String info(){
        return String.format("%s怪兽 - 生命值 : %d ", name,getHp());
    }
    public void attack(Ultraman u){
        int injury = (int) (Math.random() * 5 + 10);
        u.setHp(u.getHp() - injury);
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp < 0 ? 0 : hp;
    }
}

测试类调用两个类实现对象

package com.baidu;

public class Test03 {
    
    public static Monster selectOne(Monster[] mArray){
        Monster temp = null;
        do {
            int randomIndex = (int) (Math.random() * mArray.length);
            temp = mArray[randomIndex];
        } while (temp.getHp() == 0);
        return temp;
    }
    
    public static void showMonstersInfo(Monster[] mArray){
        for(Monster monster : mArray){
            System.out.println(monster.info());
        }
    }
    
    public static boolean isAllDead(Monster[] mArray){
        for(Monster monster : mArray){
            if(monster.getHp() > 0){
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args){
        Ultraman u = new Ultraman("迪迦", 1250, 50);
        System.out.println(u.info());
        Monster m1 = new Monster("白玉龙1号", 100);
        Monster m2 = new Monster("白玉龙2号", 100);
        Monster m3 = new Monster("白玉龙3号", 100);
        Monster m4 = new Monster("白玉龙4号", 100);
        Monster[] mArray = {m1,m2,m3,m4};
        showMonstersInfo(mArray);
        int round = 1;
        do{
            System.out.println("=======第" + round + "回合=======");
            int random = (int) (Math.random() * 10 + 1);
            Monster m = selectOne(mArray);
            if(random <= 7){
                usNormalAttack(u, m);
            }
            else if(random <= 9){
                if (u.getMp() >= 15) {
                    System.out.println("奥特曼使用了魔法攻击");
                    u.magicalAttack(mArray);
                    for (Monster monster : mArray) {
                        if (monster.getHp() > 0) {
                            monster.attack(u);
                        }
                    } 
                }
                else{
                    usNormalAttack(u, m);
                }
            }
            else {
                if(u.getMp() >= 40){
                System.out.println("奥特曼使用了终极攻击");
                u.hugeAttack(m);
                if(m.getHp() > 0){
                    m.attack(u);
                }
                }
                else{
                    usNormalAttack(u, m);               
                }
            }
            if(u.getHp() > 0){
                u.resumeMp();
            }
            round += 1;
            System.out.println(u.info());
            showMonstersInfo(mArray);
        }while(u.getHp() > 0 && !isAllDead(mArray));
        if(u.getHp() > 0){
            System.out.println("奥特曼胜利!!!");
        }
        else{
            System.out.println("小怪兽胜利!!!");
        }
    }

    public static void usNormalAttack(Ultraman u, Monster m) {
        System.out.println("奥特曼使用了普通攻击");
        u.attack(m);
        if(m.getHp() > 0){
            m.attack(u);
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读