qq_33510294 2019-05-21 14:37 采纳率: 100%
浏览 551
已采纳

求大神帮忙解答一下面对对象的编程题,万分感谢!题目如下,能写出来的直接采纳 ,不要骗人

设计一个游戏

注意:过程中定义的所有的成员变量,都要写成私有的!
定义一个装备类:
成员变量:名字、防御
构造方法:无参构造方法,带参构造方法
成员方法;设置名字(set方法)、获得名字(get方法)、设置防御、获得防御。
定义一个武器类
成员变量:名字、攻击
构造方法:无参构造方法,带参构造方法
成员方法;设置名字(set方法)、获得名字(get方法)、设置伤害、获得伤害。
定义一个角色类
成员变量:名字、血量、伤害
成员方法:设置名字(set方法)、获得名字(get方法)、设置防御、获得伤害,设置血量,获得血量。
成员方法:穿装备,穿武器。
定义一个战斗类

成员方法:相互攻击(英雄或BOSS血量为0时结束)
定义一个公共类
main方法,创建对象,执行游戏。

  • 写回答

3条回答 默认 最新

  • 发光的树 2019-05-21 17:29
    关注

    //你的问题里面角色成员变量好像没有给出防御,应该是忘记了

    package com.mansh.answer;

    //防具
    public class Equip {
    private String equipName;
    private int defend;

    public Equip(String equipName, int defend) {
        this.equipName = equipName;
        this.defend = defend;
    }
    
    public Equip() {
    }
    
    public String getEquipName() {
        return equipName;
    }
    
    public void setEquipName(String equipName) {
        this.equipName = equipName;
    }
    
    public int getDefend() {
        return defend;
    }
    
    public void setDefend(int defend) {
        this.defend = defend;
    }
    

    }

    package com.mansh.answer;

    //武器
    public class Weapon {
    private String weaponName;
    private int hurt;

    public Weapon(String weaponName, int hurt) {
        this.weaponName = weaponName;
        this.hurt = hurt;
    }
    
    public Weapon() {
    }
    
    public String getWeaponName() {
        return weaponName;
    }
    
    public void setWeaponName(String weaponName) {
        this.weaponName = weaponName;
    }
    
    public int getHurt() {
        return hurt;
    }
    
    public void setHurt(int hurt) {
        this.hurt = hurt;
    }
    

    }

    package com.mansh.answer;

    //角色
    public class Role {
    private String roleName;
    private int hp;
    private int attack;
    private int defend;

    public Role(String roleName, int hp, int attack, int defend) {
        this.roleName = roleName;
        this.hp = hp;
        this.attack = attack;
        this.defend = defend;
    }
    
    public Role() {
    }
    
    public String getRoleName() {
        return roleName;
    }
    
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    
    public int getHp() {
        return hp;
    }
    
    public void setHp(int hp) {
        this.hp = hp;
    }
    
    public int getAttack() {
        return attack;
    }
    
    public void setAttack(int attack) {
        this.attack = attack;
    }
    
    public int getDefend() {
        return defend;
    }
    
    public void setDefend(int defend) {
        this.defend = defend;
    }
    
    public void addWeapon(Weapon weapon){
        this.attack += weapon.getHurt();
    }
    public void addEquip(Equip equip){
        this.defend += equip.getDefend();
    }
    

    }

    package com.mansh.answer;

    //战斗
    public class Battle {
    public static Role[] attackEach(Role[] roles){
    if(roles.length != 2){
    throw new RuntimeException("目前仅支持2个英雄互相攻击");
    }
    roles[0].setHp(roles[0].getHp()-(roles[1].getAttack()/roles[1].getDefend()));
    roles[1].setHp(roles[1].getHp()-(roles[0].getAttack()/roles[0].getDefend()));
    return roles;
    }
    }

    package com.mansh.answer;

    //测试主方法
    public class MainTest {
    public static void main(String[] args) {
    Role a = new Role("A",100,15,5);
    Role b = new Role("B",200,10,8);
    Role[] roles = {a,b};
    Equip e = new Equip("皮甲",7);
    Weapon w = new Weapon("木棍",15);
    b.addWeapon(w);
    a.addEquip(e);

        int i=1;
        while(true){
            Battle.attackEach(roles);
            System.out.println("进行了"+i+"次攻击,双方状态:\n"+a.getRoleName()+" : "+a.getHp() +"\n"+b.getRoleName()+" : "+b.getHp());
            i++;
            if(a.getHp()<=0 && b.getHp()<=0){
                System.out.println("双方平手");
                break;
            }else if (a.getHp()<=0){
                System.out.println(b.getRoleName()+"获胜");
                break;
            }
            else if (b.getHp()<=0){
                System.out.println(a.getRoleName()+"获胜");
                break;
            }
        }
    
    
    }
    

    }

    //目前是仅支持两个角色互相攻击的,扩展的话需要自己增加对应逻辑

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?