我正在为Discord服务器编写机器人,允许人们从电子表格中查找有关视频游戏的信息,包括角色可能拥有的技能。字符按基本攻击力排序,缩写为BP。
我允许按字符类过滤,并且括号放在不以该技能开头但可以通过项目获得的字符周围(这就是为什么在某些时候它会找到等于(skill + "*")
的东西。代码如下:
private String getSkillUsers(String skill, String class) {
String bp90 = "";
String bp80 = "";
String bp70 = "";
String bp60 = "";
String bp50 = "";
String bp40 = "";
String bp30 = "";
//lfChars is a ListFeed from a Google Spreadsheet containing the chart I am referencing
for (ListEntry le : Main.lfChars.getEntries())
{
CustomElementCollection cec = le.getCustomElements();
if (type.equals(""))
{
if (cec.getValue("skill").equalsIgnoreCase(skill))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += cec.getValue("name") + ", "; break;
case 40: bp40 += cec.getValue("name") + ", "; break;
case 50: bp50 += cec.getValue("name") + ", "; break;
case 60: bp60 += cec.getValue("name") + ", "; break;
case 70: bp70 += cec.getValue("name") + ", "; break;
case 80: bp80 += cec.getValue("name") + ", "; break;
case 90: bp90 += cec.getValue("name") + ", "; break;
}
}
else if (cec.getValue("skill").equalsIgnoreCase(skill + "*"))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += "(" + cec.getValue("name") + "), "; break;
case 40: bp40 += "(" + cec.getValue("name") + "), "; break;
case 50: bp50 += "(" + cec.getValue("name") + "), "; break;
case 60: bp60 += "(" + cec.getValue("name") + "), "; break;
case 70: bp70 += "(" + cec.getValue("name") + "), "; break;
case 80: bp80 += "(" + cec.getValue("name") + "), "; break;
case 90: bp90 += "(" + cec.getValue("name") + "), "; break;
}
}
}
else
{
if (cec.getValue("skill").equalsIgnoreCase(skill) && cec.getValue("type").equalsIgnoreCase(type))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += cec.getValue("name") + ", "; break;
case 40: bp40 += cec.getValue("name") + ", "; break;
case 50: bp50 += cec.getValue("name") + ", "; break;
case 60: bp60 += cec.getValue("name") + ", "; break;
case 70: bp70 += cec.getValue("name") + ", "; break;
case 80: bp80 += cec.getValue("name") + ", "; break;
case 90: bp90 += cec.getValue("name") + ", "; break;
}
}
else if (cec.getValue("skill").equalsIgnoreCase(skill + "*") && cec.getValue("type").equalsIgnoreCase(type))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += "(" + cec.getValue("name") + "), "; break;
case 40: bp40 += "(" + cec.getValue("name") + "), "; break;
case 50: bp50 += "(" + cec.getValue("name") + "), "; break;
case 60: bp60 += "(" + cec.getValue("name") + "), "; break;
case 70: bp70 += "(" + cec.getValue("name") + "), "; break;
case 80: bp80 += "(" + cec.getValue("name") + "), "; break;
case 90: bp90 += "(" + cec.getValue("name") + "), "; break;
}
}
}
}
String skillUsers = "";
if (!bp90.equals(""))
{
skillUsers += "90: " + bp90.substring(0, bp90.length() - 2) + Main.LS;
}
if (!bp80.equals(""))
{
skillUsers += "80: " + bp80.substring(0, bp80.length() - 2) + Main.LS;
}
if (!bp70.equals(""))
{
skillUsers += "70: " + bp70.substring(0, bp70.length() - 2) + Main.LS;
}
if (!bp60.equals(""))
{
skillUsers += "60: " + bp60.substring(0, bp60.length() - 2) + Main.LS;
}
if (!bp50.equals(""))
{
skillUsers += "50: " + bp50.substring(0, bp50.length() - 2) + Main.LS;
}
if (!bp40.equals(""))
{
skillUsers += "40: " + bp40.substring(0, bp40.length() - 2) + Main.LS;
}
if (!bp30.equals(""))
{
skillUsers += "30: " + bp30.substring(0, bp30.length() - 2) + Main.LS;
}
return skillUsers;
每当我想要改变某些东西时,这很难编辑。有什么方法可以缩短它吗?如有必要,我可以添加额外的方法。
答案 0 :(得分:1)
您可以创建名为Map
的{{1}},而不是bp30-bp90。
baseAttackPower
然后你可以像这样输入并输入条目:
Map<String, String> baseAttackPowers = new HashMap<>();
您可以执行以下操作,而不是初始切换:
baseAttackPowers.put("123", "456");
您可以以类似的方式压缩if语句链。