1

I am just a little bit confused as of what to do. I have Two Weapons Classes. One for the M16 and another for the M4. I then have those Classes implementing an interface named Armory. But I am having issues with the Combat class. In the combat class I have a Random number Generator that will generate a random number and depending on what number it is, will either give the player a weapon or do nothing. I will post the Code Below:

Interface:

public interface Armory {

public Integer weaponAmmo(int wepAmmo);
public Integer weaponDamage(int wepDamage);
public String weaponName(String wepName);   
}

M4 Class(M4 and M16 Classes are the same except for damage and ammo amounts):

public class M4 implements Armory {

public Integer weaponAmmo(int wepAmmo) {
    wepAmmo = 10;
    return wepAmmo;
}

public Integer weaponDamage(int wepDamage) {
    wepDamage = 2;
    return wepDamage;
}

public String weaponName(String wepName) {
    wepName = "M4";
    return wepName;
}

And Finally, the Combat Class(This is where I am having Issues):

public class Combat {
final int chanceOfDrop = 3;

Weapons[] wepArray = {new M4(), new M16()};  //Issues here.. Don't really know how to implement this.

static boolean[] hasWeapon = {false, true};



public static int  ranNumberGen(int chanceOfDrop) {
    return (int) (Math.random()*1); 
}

private void enemyDead() {
    boolean canDrop = false;
    if(ranNumberGen(chanceOfDrop)==0){
        canDrop = true;

    }

    if(canDrop == true){
        givePlayerWeapon(wepArray[Combat.ranNumberGen(wepArray.length)] } //Issues here also.
    private static void givePlayerWeapon(int w) {
        hasWeapon[w] = true;

        for (int i = 0; i < hasWeapon.length; ++i)
        {
            if (hasWeapon[i]) System.out.println(( wepArray[i]).weaponName());  //And, last but not least, I am having Issues here
        }
            }



}

NOTE: I have a Weapons Class, But nothing is in it. I don't really know what to put in it.

Any Suggestions?

Thanks in advance: Shandan

4

1 回答 1

0

几个问题 -


A. 要将 m16 和 m14 元素放入武器数组中,这些类必须扩展(如果武器是类)或实现(如果武器是接口)武器。
另一种选择是 Weapons toWeapons()在 M16 和 M14 类中都有一个方法。

B. 如果我错了,请纠正我(不是以英语为母语的人 - 但军械库是一个提供武器的地方,所以你选择的名字不好
。M16 和 M14 应该实现一个名为“Weapon”的接口,而这个(以我的谦虚意见)应该是数组的类型

。C.如果我理解,你想在某些情况下不向用户提供武器 ​​-
完成此操作的一种方法是让 NoWeapon 类实现武器(在您当前的代码中 - 实现军械库),而不是变得丑陋(检查是否存在)。
它的方法将具有“什么都不做”的应用意义。
例如 - WeaponAmmo 将始终返回 0。

于 2012-12-23T08:51:13.147 回答