gpt4 book ai didi

java - 数组问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:42 25 4
gpt4 key购买 nike

我在开发 Bukkit 插件时遇到了数组问题。为什么这不起作用?它应该检查玩家是否已经放置了方 block 。它一直在说“钻石!!”游戏中。

@EventHandler
public void OnPlaceofDiamond(BlockPlaceEvent e){
Player player = e.getPlayer();
String storage[] = new String[100];
int i = 0;

if(e.getBlock().getType() == Material.DIAMOND_BLOCK){
if(Arrays.asList(storage).contains(player.getName())){
player.sendMessage(ChatColor.BLUE + "You are on the list");
}else{
player.sendMessage(ChatColor.BLUE + "DIAMONDS!!");
storage[i] = player.getName();
i++;
}
}
}

最佳答案

这是因为每次玩家放置方 block 时都会创建一个新的存储数组:

@EventHandler
public void OnPlaceofDiamond(BlockPlaceEvent e){
Player player = e.getPlayer();
String storage[] = new String[100];

所以你永远不会有完整的球员名单。要解决此问题,您应该在方法外部声明 Array:

String storage[] = new String[100];

@EventHandler
public void OnPlaceofDiamond(BlockPlaceEvent e) {
Player player = e.getPlayer();
int i = 0;

if(e.getBlock().getType() == Material.DIAMOND_BLOCK){
if(Arrays.asList(storage).contains(player.getName())){
player.sendMessage(ChatColor.BLUE + "You are on the list");
}
else{
player.sendMessage(ChatColor.BLUE + "DIAMONDS!!");
storage[i] = player.getName();
i++;
}
}
}

关于java - 数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22883739/

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