Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用bukkit创建计算器?_Java_Calculator_Bukkit - Fatal编程技术网

Java 如何用bukkit创建计算器?

Java 如何用bukkit创建计算器?,java,calculator,bukkit,Java,Calculator,Bukkit,我只是在学习bukkit,我想做一个计算器,就像我用所有新的编程语言(我知道基本Java)做的那样,但我似乎找不到一种方法来添加有人输入的参数,我也找不到bukkit的教程。还有谁能帮我分析一下这些数字,如果你试图添加一些非双精度的东西,它会给出不同的错误信息。我尝试添加一个switch语句来执行此操作,但没有成功 这就是我到目前为止得到的 public boolean onCommand(CommandSender sender, Command cmd, String commandLabe

我只是在学习bukkit,我想做一个计算器,就像我用所有新的编程语言(我知道基本Java)做的那样,但我似乎找不到一种方法来添加有人输入的参数,我也找不到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;
}

}您可以这样做:

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
,它会为你把这些数字加在一起

我编辑了你的代码,现在它工作了

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;
}
public boolean onCommand(命令发送方、命令cmd、字符串标签、字符串[]args){
if(cmd.getName().equalsIgnoreCase(“calc”){//如果命令为/calc
if(args.length==3){//如果命令结构类似于/calc
if(args[1].equalsIgnoreCase(“+”){//如果运算符为+
双数1=0;
双数2=0;
双结果=0;
试试{//看看第一个和第二个数字是否是双精度的
number1=Double.parseDouble(args[0]);
number2=Double.parseDouble(args[2]);
}catch(异常e){//如果这两个参数不是数字
s、 sendMessage(ChatColor.RED+“参数”+ChatColor.DARK_RED+args[0]+ChatColor.RED
+或参数“+ChatColor.DARK_RED+args[2]+ChatColor.RED”
+“不是数字!”;
return false;//代码在此停止
}
result=number1+number2;//执行此项操作
s、 sendMessage(ChatColor.GREEN+“”+number1+“+”+number2+“=”+结果);//用结果向播放机发送消息
}else if(args[1].equalsIgnoreCase(“*”){//与adition相同
双数1=0;
双数2=0;
双结果=0;
试一试{
number1=Double.parseDouble(args[0]);
number2=Double.parseDouble(args[2]);
}捕获(例外e){
s、 sendMessage(ChatColor.RED+“参数”+ChatColor.DARK_RED+args[0]+ChatColor.RED
+或参数“+ChatColor.DARK_RED+args[2]+ChatColor.RED”
+“不是数字!”;
返回false;
}
结果=数字1*数字2;
s、 sendMessage(ChatColor.GREEN+“”+number1+“*”+number2+“=”+结果);
}else if(args[1].equalsIgnoreCase(“/”)| | args[1].equalsIgnoreCase(“:”){//与adition相同
双数1=0;
双数2=0;
双结果=0;
试一试{
number1=Double.parseDouble(args[0]);
number2=Double.parseDouble(args[2]);
}捕获(例外e){
s、 sendMessage(ChatColor.RED+“参数”+ChatColor.DARK_RED+args[0]+ChatColor.RED
+或参数“+ChatColor.DARK_RED+args[2]+ChatColor.RED”
+“不是数字!”;
返回false;
}
结果=编号1/编号2;
s、 sendMessage(ChatColor.GREEN+“”+number1+args[1]+number2+“=”+结果);
}else if(args[1].equalsIgnoreCase(“-”){//与adition相同
双数1=0;
双数2=0;
双结果=0;
试一试{
number1=Double.parseDouble(args[0]);
number2=Double.parseDouble(args[2]);
}捕获(例外e){
s、 sendMessage(ChatColor.RED+“参数”+ChatColor.DARK_RED+args[0]+ChatColor.RED
+或参数“+ChatColor.DARK_RED+args[2]+ChatColor.RED”
+“不是数字!”;
返回false;
}
结果=编号1-编号2;
s、 sendMessage(ChatColor.GREEN+“”+number1+“-”+number2+“=”+结果);
}否则{
s、 sendMessage(ChatColor.RED+“无法识别操作员!”);
s、 sendMessage(ChatColor.RED+“请使用”+ChatColor.WHITE+“/calc number+number”
+ChatColor.RED+”或“+ChatColor.WHITE+”/calc number-number”+ChatColor.RED
+或“+ChatColor.WHITE+”/calc number*number“+ChatColor.RED+”或
+ChatColor.WHITE+“/calc number:number”+ChatColor.WHITE+“获取结果!”);
}
}否则{
s、 sendMessage(ChatColor.RED+“用法-/calc number运算符”);
}
}
返回false;
}

读起来真让人困惑。还有,为什么会有一个if(true),你也许可以去掉它。您得到的错误是什么?if(true)存在,因为我计划稍后在那里放置其他内容。当我尝试做加法和(我还没有做减法)时,我收到了“语法不正确”的消息,请发布你得到的确切错误(复制并粘贴),实际上我现在看到了一个问题。。。n1=args[0]将是一个数字(最好),如果(args[0]。equalsIgnoreCase(+)或(-),则向下两行。您是否一直在使用正确的args[]索引?哦,另一个。您正在比较args.length==1。这意味着唯一的索引是args[0],然后尝试读取args[1],这将抛出ArrayIndexOutOfBoundsException,因为这需要args.length为2记录所做的更改