java实验:从命令行输入两个操作数和运算符,运算符为:+、-、*、/中的一个,程序输出运算结果。
publicclassCalculator{publicstaticvoidmain(String[]args){Doublex1=Double.valueOf(args...
public class Calculator
{
public static void main(String[] args)
{
Double x1=Double.valueOf(args[0]);
String fu=args[1];
Double x2=Double.valueOf(args[2]);
Double sum=0.0;
char ch='0';
if(fu.length()>1)
{
System.out.println("运算符号错误");
System.exit(0);
}
else
{
ch=fu.charAt(0);
}
switch(ch)
{
case '+': sum=x1+x2; System.out.println("sum:"+sum);break;
case '-': sum=x1-x2; System.out.println("sum:"+sum);break;
case '*': sum=x1*x2; System.out.println("sum:"+sum);break;
case '/': sum=x1/x2; System.out.println("sum:"+sum);break;
default: System.out.println("sorry!I cant help you");
}
}
}
加减除都没问题。乘法就报错:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Calculator.java"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at Calculator.main(Calculator.java:7) 展开
{
public static void main(String[] args)
{
Double x1=Double.valueOf(args[0]);
String fu=args[1];
Double x2=Double.valueOf(args[2]);
Double sum=0.0;
char ch='0';
if(fu.length()>1)
{
System.out.println("运算符号错误");
System.exit(0);
}
else
{
ch=fu.charAt(0);
}
switch(ch)
{
case '+': sum=x1+x2; System.out.println("sum:"+sum);break;
case '-': sum=x1-x2; System.out.println("sum:"+sum);break;
case '*': sum=x1*x2; System.out.println("sum:"+sum);break;
case '/': sum=x1/x2; System.out.println("sum:"+sum);break;
default: System.out.println("sorry!I cant help you");
}
}
}
加减除都没问题。乘法就报错:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Calculator.java"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at Calculator.main(Calculator.java:7) 展开
2个回答
展开全部
你可以从如下网址的内容找到你的答案:http://zhidao.baidu.com/question/92589070.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |