java简单的面向对象练习_文字对决(路飞 vs 凯多)

 javaBean 主体代码

 

import java.util.Random;

public class Role {
    private String name;
    private int blood;
    //路飞的技能
    String[] attacks_lufei = {"%s使用了橡胶巨人手枪攻击了%s", "%s使用了橡胶雷神枪攻击了%s", "%s使用了流樱霸气缠绕攻击了%s", "%s使用了霸王色施压攻击了%s"};

    //凯多的技能
    String[] attacks_kaiduo = {"%s使用了雷鸣八卦击打了%s", "%s使用了热息攻击了%s", "%s使用了轮回天冲攻击了%s", "%s使用了罡气攻击了%s"};
    String[] injured = {"%s接住了%s的攻击并毫发无损", "%勉强的抗住了%s的伤害", "%s被%s的伤害打得泄气吐血", "%s被%s重伤"};

    public Role() {
    }

    public Role(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public String[] getAttacks_lufei() {
        return attacks_lufei;
    }

    public void setAttacks_lufei(String[] attacks_lufei) {
        this.attacks_lufei = attacks_lufei;
    }

    public String[] getAttacks_kaiduo() {
        return attacks_kaiduo;
    }

    public void setAttacks_kaiduo(String[] attacks_kaiduo) {
        this.attacks_kaiduo = attacks_kaiduo;
    }

    public String[] getInjured() {
        return injured;
    }

    public void setInjured(String[] injured) {
        this.injured = injured;
    }

    //路飞的伤害技能
    public void attack1(Role role) {

        Random r = new Random();

        int index = r.nextInt(attacks_lufei.length);
        String ki = attacks_lufei[index];

        //攻击效果
        System.out.printf(ki, this.getName(), role.getName());
        System.out.println(" ");


        //定义造成伤害的值
        int hurt = r.nextInt(99) + 1;

        //修改被攻击者的血量
        int remainBlood = role.getBlood() - hurt;

        remainBlood = remainBlood < 0 ? 0 : remainBlood;

        role.setBlood(remainBlood);

        //受伤的描述
        if (remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[0], role.getName(), this.getName());
            System.out.println(" ");


        } else if (remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[1], role.getName(), this.getName());
            System.out.println(" ");


        } else if (remainBlood > (remainBlood * 0.4) && remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[2], role.getName(), this.getName());
            System.out.println(" ");

        } else {

            System.out.printf(injured[3], role.getName(), this.getName());
            System.out.println(" ");

        }
        System.out.println("血量值还剩下" + getBlood());


    }

    //凯多的伤害技能
    public void attack2(Role role) {

        Random r = new Random();

        int index = r.nextInt(attacks_lufei.length);
        String ki = attacks_kaiduo[index];

        //攻击效果
        System.out.printf(ki, this.getName(), role.getName());
        System.out.println(" ");

        //定义造成伤害的值
        int hurt = r.nextInt(99) + 1;

        //修改被攻击者的血量
        int remainBlood = role.getBlood() - hurt;

        remainBlood = remainBlood < 0 ? 0 : remainBlood;

        role.setBlood(remainBlood);

        //受伤的描述
        if (remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[0], role.getName(), this.getName());
            System.out.println(" ");


        } else if (remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[1], role.getName(), this.getName());
            System.out.println(" ");


        } else if (remainBlood > (remainBlood * 0.4) && remainBlood > (remainBlood * 0.7) && remainBlood > (remainBlood * 0.9)) {

            System.out.printf(injured[2], role.getName(), this.getName());
            System.out.println(" ");

        } else {

            System.out.printf(injured[3], role.getName(), this.getName());
            System.out.println(" ");

        }
        System.out.println("血量值还剩下" + getBlood());


    }
}

 测试代码

public class Game_test {
    public static void main(String[] args) {
        //第一个角色
        Role j1 = new Role("尼卡觉醒*路飞",4000);
        //第二个角色
        Role j2 = new Role("龙形态*凯多",4000);

        //回合制开始战斗
        while (true){
            //路飞攻击凯多
            j1.attack1(j2);
            //凯多剩余血量
            if(j2.getBlood() == 0){
                System.out.println(j2.getName()+"被击败");
                break;
            }

            //凯多攻击路飞
            j2.attack2(j1);
            if(j1.getBlood() == 0){
                System.out.println(j1.getName()+"被击败");
                break;
            }

        }

    }
}

执行效果 

 

本练习较为简单,也有许多不足之处,由于制作时间较短 许多地方后续将会改进。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

open_test01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值