我只是在学习bukkit,我想制作一个像所有新编程语言一样的计算器(我知道基本的Java),但我似乎无法找到一种方法来添加某人输入的args,而我无法做到这一点。找到关于bukkit的教程。也可以任何人帮我解析数字,所以如果你尝试添加不是双倍的东西,它会给出不同的错误信息。我尝试添加一个switch语句来执行此操作但是没有用。
这是我到目前为止所得到的......
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
@SuppressWarnings("unused")
Player player = (Player) sender;
if(commandLabel.equalsIgnoreCase("calc")){
if(args.length == 1){
String n1 = args[0];
if (true){
if (args[0].equalsIgnoreCase("+") || args[0].equalsIgnoreCase("-")){
String n2 = args[1];
if (args[0].equalsIgnoreCase("+")){
double answer = Double.parseDouble(n1) + Double.parseDouble(n2);
sender.sendMessage("The answer is " + answer);
}
}else{
sender.sendMessage("Please use + or -");
}
}
}else{
sender.sendMessage(ChatColor.RED + "Incorrect Syntax");
}
}
return false;
}
}
答案 0 :(得分:0)
你可以这样做:
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if(cmd.getName().equalsIgnoreCase("add")){//if the comand is /add
if(args.length == 2){// if there are two arguments
try{
long n1 = Long.parseLong(args[0]);//get the first number
long n2 = Long.parseLong(args[1]);//get the second number
long result = n1 + n2;//add the two numbers together
sender.sendMessage(n1 + " + " + n2 + " equals " + result);//tell the sender the result
}
catch(Exception e){
//the user did not enter numbers
}
}
else{
sender.sendMessage("usage: /add num1 num2");
}
}
return true;
}
return false;
}
然后你可以/add <number 1> <number 2>
,它会为你添加这些数字
答案 1 :(得分:0)
我编辑了你的代码,现在它正在运行!
public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("calc")) { //if the command is /calc
if (args.length == 3) { // if the command structure is something like this /calc <number> <operator> <number>
if (args[1].equalsIgnoreCase("+")) { // if the operator is +
double number1 = 0;
double number2 = 0;
double result = 0;
try { // see if the first and second numbers are a double
number1 = Double.parseDouble(args[0]);
number2 = Double.parseDouble(args[2]);
} catch (Exception e) { // if the two arguments aren't numbers
s.sendMessage(ChatColor.RED + "The argument " + ChatColor.DARK_RED + args[0] + ChatColor.RED
+ " or the argument " + ChatColor.DARK_RED + args[2] + ChatColor.RED
+ " is not a number !");
return false; // the code stops here
}
result = number1 + number2; // doing the adition
s.sendMessage(ChatColor.GREEN + "" + number1 + " + " + number2 + " = " + result); // send message to the player with the resut
} else if (args[1].equalsIgnoreCase("*")) { //same as adition
double number1 = 0;
double number2 = 0;
double result = 0;
try {
number1 = Double.parseDouble(args[0]);
number2 = Double.parseDouble(args[2]);
} catch (Exception e) {
s.sendMessage(ChatColor.RED + "The argument " + ChatColor.DARK_RED + args[0] + ChatColor.RED
+ " or the argument " + ChatColor.DARK_RED + args[2] + ChatColor.RED
+ " is not a number !");
return false;
}
result = number1 * number2;
s.sendMessage(ChatColor.GREEN + "" + number1 + " * " + number2 + " = " + result);
} else if (args[1].equalsIgnoreCase("/") || args[1].equalsIgnoreCase(":")) { //same as adition
double number1 = 0;
double number2 = 0;
double result = 0;
try {
number1 = Double.parseDouble(args[0]);
number2 = Double.parseDouble(args[2]);
} catch (Exception e) {
s.sendMessage(ChatColor.RED + "The argument " + ChatColor.DARK_RED + args[0] + ChatColor.RED
+ " or the argument " + ChatColor.DARK_RED + args[2] + ChatColor.RED
+ " is not a number !");
return false;
}
result = number1 / number2;
s.sendMessage(ChatColor.GREEN + "" + number1 + args[1] + number2 + " = " + result);
} else if (args[1].equalsIgnoreCase("-")) { //same as adition
double number1 = 0;
double number2 = 0;
double result = 0;
try {
number1 = Double.parseDouble(args[0]);
number2 = Double.parseDouble(args[2]);
} catch (Exception e) {
s.sendMessage(ChatColor.RED + "The argument " + ChatColor.DARK_RED + args[0] + ChatColor.RED
+ " or the argument " + ChatColor.DARK_RED + args[2] + ChatColor.RED
+ " is not a number !");
return false;
}
result = number1 - number2;
s.sendMessage(ChatColor.GREEN + "" + number1 + " - " + number2 + " = " + result);
} else {
s.sendMessage(ChatColor.RED + "Operator not recognized !");
s.sendMessage(ChatColor.RED + "Please use " + ChatColor.WHITE + "/calc number + number"
+ ChatColor.RED + " OR " + ChatColor.WHITE + "/calc number - number" + ChatColor.RED
+ " OR " + ChatColor.WHITE + "/calc number * number" + ChatColor.RED + " OR "
+ ChatColor.WHITE + "/calc number : number" + ChatColor.WHITE + " to get a result !");
}
} else {
s.sendMessage(ChatColor.RED + "Usage - /calc number operator number ");
}
}
return false;
}