-2

这是如何从java中的另一个方法正确调用一个方法吗?有人可以描述如何创建一个重载的攻击方法,允许我使用基本攻击修饰符运行攻击吗?actionPerformed 是控制以正确的 java swing 样式实例化的所有按钮的提交操作的方法。该命令取决于按钮,该程序将帮助模拟 rpg 的第一次战斗。攻击函数调用所有布尔函数来接收按钮提交结果的各种可能性。actionPerformed 函数应该调用损坏函数,该函数应该更新 Jtable 中的 hp 值。附上演示所描述功能的代码。如果有人可以帮助我,我需要一些关于代码故障排除的帮助,我将不胜感激。

public void actionPerformed(ActionEvent event) 
{
    String command = event.getActionCommand();

    this.getRows(table);

    rows = this.rows;

    firstRow = rows[0];

    lastRow = rows[1];

    if(command == "Shield Bash") {
        this.attack(firstRow, lastRow, command, table);
        damage = this.damage;
        this.damage(table, firstRow, damage);
    }else if(command == "Run Threw") {

    }else if(command == "React") {
        this.attack(firstRow, lastRow, command, table);
        damage = this.damage;
        this.damage(table, firstRow, damage);
    }else if (command == "Attack") {
        this.attack(firstRow, lastRow, command, table);
        damage = this.damage;
        this.damage(table, firstRow, damage);
    }else if (command == "Skill") {

    }else if (command == "Heal") {

    }else if (command == "Rest") {

    }else if (command == "Skulk") {

    }else {

    }

}

public boolean block (JTable table, int defendersRow) {
    defendersRow = this.defendersRow;
    blockChanceObject = table.getValueAt(defendersRow, 15);
    blockChance = (Integer) blockChanceObject;
    blockRoll = generator.nextInt(100) + 1;
    if(blockRoll < blockChance) {
        blocked = true;
    }
    return blocked;
}

public boolean fumble (JTable table, int attackersRow) {
     attackersRow = this.attackersRow;
     fumbleChanceObject = table.getValueAt(attackersRow, 7);
     fumbleChance = (Integer) fumbleChanceObject;
     int fumbleRoll = generator.nextInt(100) + 1;
     if (fumbleRoll < fumbleChance) {
         fumbled = true;
     }
     return fumbled;

}

public boolean dodge (JTable table, int defendersRow) {

    defendersRow = this.defendersRow;

    dodgeChanceObject = table.getValueAt(defendersRow,12);

    dodgeChance = (Integer) dodgeChanceObject;

    dodgeRoll = generator.nextInt(100) + 1;

    if (dodgeRoll < dodgeChance) {
        dodged = true;
    }

    return dodged;

}

public boolean critical (JTable table, int attackersRow, int attackRoll) {
    attackersRow = this.attackersRow;
    attackRoll = this.attackRoll;
    criticalChanceObject = table.getValueAt(attackersRow, 8);
    criticalChance = (Integer) criticalChanceObject;
    if (attackRoll >= criticalChance) {
        criticaled = true;
    }
    return criticaled;
}

public int[] getRows(JTable table) {    
    rows[0] = table.getSelectedRow();
    rowCount = table.getSelectedRowCount() - 1;
    rows[1] = rows[0] + rowCount;
    return rows;
}

public int attack(int firstRow, int lastRow, String command, JTable table) {
    command = this.command;
    firstRow = this.firstRow;
    lastRow = this.lastRow;
    table = this.table;

    if (command == "Bludgeon" || command == "React" || command == "ShieldBash") {
        attackersRow = this.lastRow;
        defendersRow = this.firstRow;
    }else if(command == "Attack" || command == "Skill") {
        attackersRow = this.firstRow;
        defendersRow = this.lastRow;
    }else {

    }

    this.fumble(table, attackersRow);
    if (fumbled == true) {
        outputString = "fumbled";
    }

    attackRoll = generator.nextInt(100) + 1;
    this.critical(table, attackersRow, attackRoll);
    if (criticaled == true) {
        outputString = "criticaled";
    }
    this.dodge(table, defendersRow);
    if (dodged == true) {
        outputString = "dodged";
    }
    this.block(table, defendersRow);
    if (blocked == true) {
        outputString = "blocked";
    }
    defenseRoll = generator.nextInt(100) + 1;
    attackBaseObject = table.getValueAt(attackersRow, 6);
    defenseBaseObject = table.getValueAt(defendersRow, 11);
    attackBase = (Integer) attackBaseObject;
    defenseBase = (Integer) defenseBaseObject;
    attack = attackRoll + attackBase;
    defense = defenseRoll + defenseBase;
    minDamageObject = table.getValueAt(attackersRow, 9);
    minDamage = (Integer) minDamageObject;
    maxDamageObject = table.getValueAt(attackersRow, 10);
    maxDamage = (Integer) maxDamageObject;
    damage = generator.nextInt((maxDamage - minDamage))+minDamage;
    if (criticaled == true) {
        damage = maxDamage * 2;
    }else if (attack >= (defense + 50)) {
        damage = damage * 2;
    }else if (attack >= defense) {
        damage = damage;
    }else {
        damage = 0;
    }
    this.outputSelection(outputString, attackersRow, defendersRow, table, command, damage);
    return damage;
}

private void damage(JTable table, int defendersRow, int damage) {
    damage = this.damage;
    defendersRow = this.defendersRow;
    hpObject = table.getValueAt(defendersRow, 3);
    hp = (Integer) hpObject;
    hp = hp - damage;
    table.setValueAt(hp, defendersRow, 3);
}

private void outputSelection(String outputString, int attackersRow, int defendersRow, JTable table, String command, int damage) {
4

1 回答 1

2

一些快速观察 - 不要使用 == 比较字符串。使用 .equals 方法。其次,您似乎没有遵循面向对象的范例。您可能应该创建几个类,正确设计它们,然后开始您的实现。

于 2013-01-14T05:03:56.090 回答