有没有办法将ArrayList中的整数相加?

huangapple go评论41阅读模式
英文:

Is there a way to add integers from an arraylist together?

问题

我正在尝试将ArrayList中的整数相加,以便让人们看到有多少人排队进行1v1对战。这是我的代码:

package me.sub.cPractice.Queue;

Main plugin;

public JoinQueue(Main plugin) {
    this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    if (cmd.getName().equalsIgnoreCase("queuetest")) {
        Player p = (Player) sender;
        new BukkitRunnable() {
            @Override
            public void run() {
                if (plugin.inqueue.contains(p)) {
                    String replaced = PlaceholderAPI.setPlaceholders(p, "%server_online%");
                    ScoreHelper helper = ScoreHelper.createScore(p);
                    helper.setTitle("§6§lGoldHQ §r§7┃ §rPractice");
                    helper.setSlot(8, "§7§m---------------------");
                    helper.setSlot(7, "§eOnline: §f" + replaced);
                    helper.setSlot(6, "§eIn Fights: §f" + plugin.infightnumber.toString().replace("[", "").replace("]", ""));
                    helper.setSlot(5, "§7§m---------------------");
                    helper.setSlot(4, "§eQueued For: §fNoDebuff");
                    helper.setSlot(3, "" + plugin.inqueuenumber.toString().replace("[", "").replace("]", ""));
                    helper.setSlot(2, "§7§ogoldhq.net");
                    helper.setSlot(1, "§7§m---------------------");
                }
            }
        }.runTaskTimer(plugin, 20L, 20L);
    }
    return false;
}

我已经卡在这个问题上几个小时了,但没有找到有效的解决方法。它显示为1,1,但我希望它显示为2。有什么帮助吗?

英文:

I'm trying to add integers from an arraylist together to allow the person to see how many people are queued for a 1v1. This is my code: `
package me.sub.cPractice.Queue;

Main plugin;
public JoinQueue(Main plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
if (cmd.getName().equalsIgnoreCase("queuetest")) {
Player p = (Player) sender;
new BukkitRunnable() {
@Override
public void run() {
if (plugin.inqueue.contains(p)) {	
String replaced = PlaceholderAPI.setPlaceholders(p, "%server_online%");
ScoreHelper helper = ScoreHelper.createScore(p);
helper.setTitle("&6&lGoldHQ &r&7┃ &rPractice");
helper.setSlot(8, "&7&m---------------------");
helper.setSlot(7, "&eOnline: &f" + replaced);
helper.setSlot(6, "&eIn Fights: &f" + plugin.infightnumber.toString().replace("[", "").replace("]", ""));
helper.setSlot(5, "&7&m---------------------");
helper.setSlot(4, "&eQueued For: &fNoDebuff");
helper.setSlot(3, "" + plugin.inqueuenumber.toString().replace("[", "").replace("]", ""));
helper.setSlot(2, "&7&ogoldhq.net");
helper.setSlot(1, "&7&m---------------------");
}
}
}.runTaskTimer(plugin, 20L, 20L);
}
return false;
}

`
I've been stuck on this for a couple hours now, and I haven't been able to find anything that works. It displays it as 1, 1 when I want it to be displayed as 2. Any help?

答案1

得分: 2

首先,您的问题中根本没有包含ArrayList。我猜想您的问题出在以下代码行:

helper.setSlot(3, """ + plugin.inqueuenumber.toString().replace(""["", """).replace(""]"", """));

您可以使用以下代码将那些数字合并在一起:

helper.setSlot(3, """ + Arrays.stream(plugin.inqueuenumber.toString()
        .replace(""["", """")
        .replace(""]"", """")
        .split("","")
    ).map(Integer::parseInt)
    .reduce(0, (subtotal, current) -> subtotal + current)
)

编辑:我注意到plugin.inqueuenumber就是您一直在谈论的ArrayList,因此以下代码也应该起作用,前面的解决方案是多余的:

helper.setSlot(3, """ + plugin.inqueuenumber.stream().reduce(0, (l, r) -> l + r))
英文:

First of all your question doesn't contain ArrayList at all. I guess your problem is at line:

helper.setSlot(3, "" + plugin.inqueuenumber.toString().replace("[", "").replace("]", ""));

You can use following code to merge that number's together:

helper.setSlot(3, "" + Arrays.stream(plugin.inqueuenumber.toString()
.replace("[", "")
.replace("]", "")
.split(",")
).map(Integer::parseInt)
.reduce(0, (subtotal, current) -> subtotal + current)
)

Edit: i noticed that plugin.inqueuenumber is that ArrayList you have been talking about so also following should work and previous solution is redundant:

helper.setSlot(3, "" + plugin.inqueuenumber.stream().reduce(0, (l, r) -> l + r))

huangapple
  • 本文由 发表于 2020年7月29日 08:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63144470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定