Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java程序运行时,符号 * 的用法求助
java_lhl





发贴: 3
积分: 0
于 2008-07-16 20:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
下面是这段小程序:

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,小弟初学,不太明白
请问大家:这个*为什么会导致错误呢?

谢谢!



作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
billgacsli





发贴: 11
积分: 0
于 2008-07-17 11:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
"*"是特殊字符,表示匹配所有,当作为参数传入时,会实际的传入当前目录下的所有文件名
你可以通过下面代码打印参数就知端倪,至于为什么,不胜了了

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



作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-17 15:45 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
It cannot even pass the compiler??

d = d1*d2;
  ^
1 error



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
billgacsli





发贴: 11
积分: 0
于 2008-07-17 22:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
那个地方应该是变量f


作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
java_lhl





发贴: 3
积分: 0
于 2008-07-17 23:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢各位!
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 )


阿熊 edited on 2008-07-19 18:51

作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
billgacsli





发贴: 11
积分: 0
于 2008-07-18 12:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈哈
刀叉都出来了



作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-18 15:45 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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


When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-18 15:48 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Tracked by the following thread link:

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



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:java程序运行时,符号 * 的用法求助 [Re:java_lhl]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-23 23:46 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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