从界面中的数组中提取统计数据?

时间:2017-04-10 04:26:04

标签: java

在这里完全和完全诚实,我不知道如何表达我的问题,并且已经仔细阅读了几个小时的互联网来找到它,但看起来这种事情并不常见,但可以通过查看我的代码来解决。

public class Stats implements BasesGalore {


private static String getCharacterClass(int a, int d, int m, int s) {
    if (a == 0 && d == 0 && m == 0 && s == 0) {return "villager";}
    if (a == 1 && d == 0 && m == 0 && s == 0) {return "fighter";}
    if (a == 0 && d == 1 && m == 0 && s == 0) {return "guard";}
    if (a == 0 && d == 0 && m == 1 && s == 0) {return "spellcaster";}
    if (a == 0 && d == 0 && m == 0 && s == 1) {return "athlete";}
    if (a == 1 && d == 1 && m == 0 && s == 0) {return "knight";}
    if (a == 1 && d == 0 && m == 1 && s == 0) {return "spellsword";}
    if (a == 1 && d == 0 && m == 0 && s == 1) {return "martial_artist";}
    if (a == 0 && d == 1 && m == 1 && s == 0) {return "cleric";}
    if (a == 0 && d == 1 && m == 0 && s == 1) {return "escapist";}
    if (a == 0 && d == 0 && m == 1 && s == 1) {return "expediter";}
    if (a == 2 && d == 0 && m == 0 && s == 0) {return "slayer";}
    if (a == 0 && d == 2 && m == 0 && s == 0) {return "defender";}
    if (a == 0 && d == 0 && m == 2 && s == 0) {return "magician";}
    if (a == 0 && d == 0 && m == 0 && s == 2) {return "guerrilla";}
    else return "villager";
}

private static int fa = 0;
private static int fd = 0;
private static int fm = 0;
private static int fs = 0;

private static int BaseHP = getCharacterClass(fa, fd, fm, fs)[0]; // Base stats for the character's class

我的问题在最后一行。我试图从一个加载了我的角色的所有基本统计数据的界面中拉出来(每个类都有自己的特定数组及其统计数据)。我试图调用getCharacterClass来查找角色的类,然后通过数组查找其基本属性。

这里有一点界面:

public interface BasesGalore {

String[] classes = {
        "villager",
        "fighter", "guard", "spellcaster", "athlete",
        "knight", "spellsword", "martial_artist", "cleric", "escapist", "expediter", "slayer", "defender", "magician", "guerrilla"
};

int[] villager = {
        50, 50, 50, 50, 50 // 50
};

int[] fighter = {
        60, 70, 50, 35, 35 // 50
};

int[] guard = {
        80, 50, 80, 20, 40 // 54
};

int[] spellcaster = {
        65, 20, 15, 105, 60 // 53
};

int[] athlete = {
        65, 50, 50, 20, 80 // 53
};

int[] knight = {
        80, 70, 50, 35, 35 //
};

提前致谢,如果绝对没有任何理由,我真的很抱歉 NitroDragon523

1 个答案:

答案 0 :(得分:2)

老实说,OO适合你。

首先,您不应为统计数据生成数组,而应生成BaseCharacter的实例。

public class BaseCharacter{
    private String name; //just to have his name
    private int stat1; //to rename later
    private int stat2; //to rename later
    ...
    private int statX; //to rename later

    public BaseCharacter(String name, int stat1, int stat2, ..., int statX){
        this.name = name;
        this.stat1 = stat1;
        this.stat2 = stat2;
        ...
        this.statX = statX;
    }

    // add getter (and setter if the stats can be modify)
}

然后,在界面中,String[]应该变为BaseCharacter[]

interface BasesGalore {

    static final BaseCharacter[] characters = {
        new BaseCharacter("Villager",  50, 50, 50, 50, 50),
        new BaseCharacter("Fighter",  60, 70, 50, 35, 35),
        ...
    };

    ...
}

然后,你有一些选择来获得正确的实例,一个,因为这个数组在接口中,你可以用每个字符的索引列出一些常量(只有在没有多个字符的情况下才有用)

static final int VILLAGER = 0;
static final int FIGHTER = 1;

public static BaseCharacter getCharacter(int index){
    return characters[index];
}

这将像

一样使用
BasesGalore.getCharacter(BasesGalore.FIGHTER);

或者您使用每个实例的名称来查找它,如果您希望能够动态更新字符列表,这样会更好。

public static BaseCharacter getCharacter(String name);

在这里,您可以使用简单的循环来查找具有相似名称的字符。请注意,这可能会返回null。