试图压缩引用电子表格的代码
Trying to condense code that references a spreadsheet
我正在为 Discord 服务器编写一个机器人,它允许人们从电子表格中查找有关视频游戏的信息,包括角色可能拥有的技能。角色按基础攻击力排序,缩写为BP。
我允许按角色过滤 class,并且括号放在不以该技能开头但可以通过物品获得该技能的角色周围(这就是为什么在某些时候它会寻找等于 (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;
每当我想更改某些内容时,这都很难编辑。有什么办法可以缩短它吗?如有必要,我可以添加额外的方法。
您可以创建一个名为 baseAttackPower
的 Map
而不是 bp30-bp90。
Map<String, String> baseAttackPowers = new HashMap<>();
然后您可以像这样获取条目并将其放入其中:
baseAttackPowers.put("123", "456");
除了初始切换,您还可以这样做:
String value = cec.getValue("bp");
String bp = baseAttackPowers.get(value);
baseAttackPowers.put(value, bp + cec.getValue("name") + ", ");
您可以用类似的方式压缩 if 语句链。
我正在为 Discord 服务器编写一个机器人,它允许人们从电子表格中查找有关视频游戏的信息,包括角色可能拥有的技能。角色按基础攻击力排序,缩写为BP。
我允许按角色过滤 class,并且括号放在不以该技能开头但可以通过物品获得该技能的角色周围(这就是为什么在某些时候它会寻找等于 (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;
每当我想更改某些内容时,这都很难编辑。有什么办法可以缩短它吗?如有必要,我可以添加额外的方法。
您可以创建一个名为 baseAttackPower
的 Map
而不是 bp30-bp90。
Map<String, String> baseAttackPowers = new HashMap<>();
然后您可以像这样获取条目并将其放入其中:
baseAttackPowers.put("123", "456");
除了初始切换,您还可以这样做:
String value = cec.getValue("bp");
String bp = baseAttackPowers.get(value);
baseAttackPowers.put(value, bp + cec.getValue("name") + ", ");
您可以用类似的方式压缩 if 语句链。