public class Test{
public static void main(String[] args){
if(args.length<3){
System.out.println("请按照\"num\" \"operator\" \"num\"的格式输入命令行参数!");
System.exit(-1);
}else
{
try{
double x=Double.parseDouble(args[0]);
double y=Double.parseDouble(args[2]);
String op=args[1];
double result=0;
if(op.equals("+")){
result=x+y;
}else if(op.equals("-")){
result=x-y;
}else if(op.equals("*")){
result=x*y;
}else if(op.equals("/")){
result=x/y;
}else{
System.out.println("请检查你的操作符!");
}
System.out.println(result);
}catch(NumberFormatException e){
System.out.println("请检查你的两个参数的格式!");
}
}
}
}这个程序在运行的时候,为什么用*号就一定会出现异常呢?
public static void main(String[] args){
if(args.length<3){
System.out.println("请按照\"num\" \"operator\" \"num\"的格式输入命令行参数!");
System.exit(-1);
}else
{
try{
double x=Double.parseDouble(args[0]);
double y=Double.parseDouble(args[2]);
String op=args[1];
double result=0;
if(op.equals("+")){
result=x+y;
}else if(op.equals("-")){
result=x-y;
}else if(op.equals("*")){
result=x*y;
}else if(op.equals("/")){
result=x/y;
}else{
System.out.println("请检查你的操作符!");
}
System.out.println(result);
}catch(NumberFormatException e){
System.out.println("请检查你的两个参数的格式!");
}
}
}
}这个程序在运行的时候,为什么用*号就一定会出现异常呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货