浮点数怎么不能正确的输出呢????

LEESEA 2002-09-26 08:37:32
class YiWai
{public static void main(String[] args){
double rs;
final int LEN=20;
byte[] temp1=new byte[LEN];
try
{
System.out.print("input x : ");
System.in.read(temp1,0,LEN);
}
catch (Exception e){}
String xtemp=new String(temp1);
int x=new Integer(xtemp.trim()).intValue();

//the first number!
byte[] temp2=new byte[LEN];
try
{
System.out.print("input y : ");
System.in.read(temp2,0,LEN);
}
catch (Exception e){}
String ytemp=new String(temp2);
int y=new Integer(ytemp.trim()).intValue();
if (x==y)
{System.out.print("error");
}
else{
rs=(x+y)/(x-y);
System.out.print("rs is "+rs);

}
}
};

输出是有小数的
但是小数位置总是零!

...全文
140 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LEESEA 2002-09-26
  • 打赏
  • 举报
回复
明白了
也就是只要有一个数字给转换成浮点就可以了·

谢谢了·
szp 2002-09-26
  • 打赏
  • 举报
回复
上面的正确
7 2002-09-26
  • 打赏
  • 举报
回复
rs=((double)x+y)/(x-y);
dreamhead 2002-09-26
  • 打赏
  • 举报
回复
在这句
rs=(x+y)/(x-y);
里,由于x,y都是整数,运算的结果必然是整数,为什么?C语言里就是这么教育大家的:整数和整数,结果还是整数。赋值给rs当然小数为0了:)
如果想要运算正确,可以在那句运算时,将x和y转为double。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧