我正在尝试在Eclipse中“运行配置”。 当我传递“1 + 2”,或“123 - 321”或“123/321”之类的内容时效果很好。 当我尝试乘法时出现问题。在这种情况下,我得到
Exception in thread "main" java.lang.NumberFormatException: For input string: ".project"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Main.main(Main.java:15)
以下是代码:
public class Main {
public static void main(String[] args) {
double aNum;
double bNum;
char operator;
String result = "Error";
Calculator calc = new Calculator();
if (args.length == 0) {
System.out.println("No parameters were entered");
}
else {
aNum = Double.parseDouble(args[0]);
bNum = Double.parseDouble(args[2]);
operator = args[1].charAt(0);
result = calc.calculate(aNum, bNum, operator);
System.out.println(result);
}
}
}
public class Calculator {
public String calculate(double aNum, double bNum, double operator) {
String result = "Error";
if(operator=='+'){
result = String.valueOf(aNum+bNum);
}
else if (operator=='-') {
result = String.valueOf(aNum-bNum);
}
else if (operator=='*') {
result = String.valueOf(aNum*bNum);
}
else if (operator=='/') {
if (bNum==0) {
System.out.println("Forbidden operation: div by zero!");
}
else {
result = String.valueOf(aNum/bNum);
}
}
else {
System.out.println("Unhandled operator. Please use '+-*/' as operators");
result = "Error";
}
return result;
}
}
答案 0 :(得分:4)
问题是你如何调用该程序。如果您运行:
java Calculator 5 * 10
然后在一些命令shell中,*
将自动扩展到当前目录中的所有文件名。您应该能够通过引用来解决这个问题,例如
java Calculator 5 '*' 10
或者从计算器中询问值,而不是从命令行中获取它们。