我是初学者,有个小问题求助:
/*创建一个程序,用于将两个整数值和一个运算符存储在相应的变量中,并执行必要的运算(使用switch语句)*/
class Operation1
{
public static void main (String args[])
{
int num = 0, num1 = Integer.parseInt (args[0]), num2 = Integer.parseInt (args[2]);
char operator1 = args[1].charAt(0);
switch (operator1)
{
case '+':
num = num1 + num2;
break;
case '-':
num = num1 - num2;
break;
case '*':
num = num1 * num2;
break;
case '/':
num = num1 / num2;
break;
case '%':
num = num1 % num2;
break;
}
System.out.print (String.valueOf (num1) + operator1 + String.valueOf (num2) + "=" + num);
}
}
程序内容如上;
但是运行的时候
如果我输入java Operation1 3 * 5
提示:
Exception in thread "main" java.lang.NumberFormatException:For input string:"Operation1.java"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Operation1.main(Operation1.java:7)
而如果“*”号用“+、-、/、%”代替则可以正常运行,请哪位高手有空的话帮个忙回答一下哈!