2013-01-14 229 views
-2

这是如何正确调用java中的另一种方法的方法?有人可以描述如何创建一个超载的攻击方法,使我能够使用基础攻击修改器运行攻击吗? actionPerformed是控制所有按照适当的java swing风格实例化的按钮的提交动作的方法。该命令是依赖于按钮的,程序是帮助模拟RPG的第一场战斗。攻击函数调用所有布尔函数来接收按钮提交结果的各种可能性。 actionPerformed函数应该调用应该更新Jtable中hp值的损坏函数。演示所描述功能的代码已附加。如果有人可以帮助我,我需要一些关于代码故障排除的帮助,我将不胜感激。从java中的方法调用方法

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) { 
+0

阅读[如何比较Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo

回答

2

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

+0

你能进一步深入细节?我想进一步讨论你的想法?谢谢你的时间。祝你今天愉快。 – user1956201