Topic: java程序运行时,符号 * 的用法求助

  Print this page

1.java程序运行时,符号 * 的用法求助 Copy to clipboard
Posted by: java_lhl
Posted on: 2008-07-16 20:43

下面是这段小程序:

public class Args {
  public static void main(String[] args) {

double d1 = Double.parseDouble(args[0]);
double d2 = Double.parseDouble(args[2]);
double f = 0;
if(args[1].equals("+")) f= d1+d2;
else if(args[1].equals("*")) d = d1*d2;
  else{
    f = 10;
  }

System.out.println(f);
  }
}

这是运行后的结果:

E:\java>java Args 3 * 2
Exception in thread "main" java.lang.NumberFormatException: For input string: "A
rgs.class"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:12
24)
at java.lang.Double.parseDouble(Double.java:510)
at Args.main(Args.java:5)

但如果把*换成字符x就能算出来正确结果为: 6.0,小弟初学,不太明白
请问大家:这个*为什么会导致错误呢?

谢谢!

2.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: billgacsli
Posted on: 2008-07-17 11:05

"*"是特殊字符,表示匹配所有,当作为参数传入时,会实际的传入当前目录下的所有文件名
你可以通过下面代码打印参数就知端倪,至于为什么,不胜了了

for(int i = 0; i < args.length; i++){
System.out.println(args[i]);
}

3.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-17 15:45

It cannot even pass the compiler??

d = d1*d2;
  ^
1 error

4.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: billgacsli
Posted on: 2008-07-17 22:01

那个地方应该是变量f

5.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: java_lhl
Posted on: 2008-07-17 23:45

谢谢各位!
public class Args{
  public static void main(String[] args){
    double d1 = Double.parseDouble(args[0]);
    double d2 = Double.parseDouble(args[2]);
    double d = 0;
    
    if(args[1].equals("+")) d = d1 + d2;
    else if (args[1].equals("x")) d = d1*d2;
    else return;
    
    System.out.println(d);
  }
}

在这个论坛里 d加括号容易出现别的问题,所以我换了字母,这是原来的,最后的输出语句是( d )

6.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: billgacsli
Posted on: 2008-07-18 12:38

哈哈
刀叉都出来了

7.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-18 15:45

It is an issue here, I will forward this information to the web admin to have a look. The parenthesis does annoy me sometimes as well. Smile

8.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-18 15:48

Tracked by the following thread link:

http://www.cjsdn.net/post/view?bid=15&id=195119&sty=1#195119

9.Re:java程序运行时,符号 * 的用法求助 [Re: java_lhl] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-23 23:46

There is a similar solution provided by 阿熊 available at the following link:

http://www.cjsdn.net/post/view?bid=15&id=195119&sty=1#195119


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923