gpt4 book ai didi

java - 在界面中从数组中提取统计信息?

转载 作者:行者123 更新时间:2023-11-30 02:35:03 25 4
gpt4 key购买 nike

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

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 //
};

提前致谢,如果完全没有意义,我真的很抱歉,硝基龙523

最佳答案

说实话,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。

关于java - 在界面中从数组中提取统计信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43314977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com