2013-01-14 181 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