小小大戰

楊怡寶發表於2020-09-28

HeroDemo類

package com.zhcpt.sixtest;

import java.util.Random;

public class HeroDemo {
    public static void main(String[] args) {

        int[] SkillHurt ={50,60,70,100};

        String[] Skillcxk = {"唱","跳","Rap","籃球"};
        Hero cxk = new Hero("蔡先生",Skillcxk,SkillHurt,700);

        String[] Skillwyf = {"吹","拉","談","skr"};
        Hero wyf = new Hero("吳帥氣",Skillwyf,SkillHurt,700);

        Random random = new Random();
        int i = random.nextInt(2);
        System.out.println("開始對戰,搖骰子決定誰先開始");
        while (true){
            if (i==0){
                System.out.println("蔡先生先出手攻擊吳帥氣");
                if (cxk.getHp()<=0){
                    System.out.println(cxk.getName()+"血量低於0,無法繼續攻擊"+wyf.getName()+"獲勝");
                    break;
                }
                cxk.attack(wyf);
                if (wyf.getHp() <= 0) {
                    System.out.println(wyf.getName() + "xiedi" + cxk.getName() + "獲勝");
                }
                wyf.attack(cxk);
            } else if (i == 1) {
                System.out.println("吳亦凡開始攻擊蔡徐坤");
                if (wyf.getHp() <= 0) {
                    System.out.println(wyf.getName() + "血量低,無法攻擊" + cxk.getName() + "獲勝");
                    break;
                }
                wyf.attack(cxk);

                if (cxk.getHp() <= 0) {
                    System.out.println(cxk.getName() + "血量為0" + wyf.getName() + "獲勝");
                }
                cxk.attack(wyf);
            }
        }
    }
}

Hero類

package com.zhcpt.sixtest;

import java.util.Random;

public class Hero {
    private String Name;
    private String[] Skill;
    private int[] SkillHurts;
    private double Hp;

    public Hero() {
    }

    public Hero(String name, String[] skill, int[] skillHurts, double hp) {
        Name = name;
        Skill = skill;
        SkillHurts = skillHurts;
        Hp = hp;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String[] getSkill() {
        return Skill;
    }

    public void setSkill(String[] skill) {
        Skill = skill;
    }

    public int[] getSkillHurts() {
        return SkillHurts;
    }

    public void setSkillHurts(int[] skillHurts) {
        SkillHurts = skillHurts;
    }

    public double getHp() {
        return Hp;
    }

    public void setHp(double hp) {
        Hp = hp;
    }
    public  void attack(Hero otherHero) {
        Random random = new Random();
        int SkillNum = random.nextInt(Skill.length);
        String Skill = this.Skill[SkillNum];
        double SkillHurt = otherHero.SkillHurts[SkillNum];
        otherHero.Hp = otherHero.Hp - SkillHurt;
        System.out.println(this.Name + "使用了" + Skill + "技能,對"
                + otherHero.getName() + "造成了" + SkillHurt
                + "傷害" + otherHero.getName() + "現在還剩" + otherHero.getHp() + "血量");
    }
}

相關文章