使用
Bukkit.getServer().broadcastMessage(ChatColor.RED + args[0] + " " + args[1] + " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + args[6] + " " + args[7] + " " + args[8] + " " + args[9] + " " + args[10] + " " + args[11] + " " + args[12] + " " + args[13] + " " + args[14]);
如果恰好有15參數纔有效。如果參數較少,則會拋出一個ArrayIndexOutOfBoundsException
,因爲您試圖訪問不存在的數組的一部分。如果有更多的參數,代碼將只打印前15個參數,其餘的將被忽略。
要解決這個問題,就需要通過所有的參數循環:
for(String argument : args)
然後,你需要將參數添加到廣播消息,與空間一起:
message+=argument;
message+=" ";
爲避免ArrayIndexOutOfBoundsException
,您還應該檢查是否至少有一個參數:
if(args.length >= 1)
所以,這裏是你的代碼可能是什麼樣子:
if (args.length >= 1) { // make sure there is at least 1 argument to avoid an ArrayOutOfBoundsException
String message = ""; // initialize the "message" variable
for (String argument : args) { // loop through all of the arguments
message += argument; // add the argument to the message
message += " "; // add a space to the message
}
Bukkit.getServer().broadcastMessage(ChatColor.RED + message); // broadcast the message
}