在我的编码中有错误,这可能是从double到int的有损转换 [英] In my coding have error which is possible lossy conversion from double to int

查看:90
本文介绍了在我的编码中有错误,这可能是从double到int的有损转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(unit> = 1&& unit< = 200){

rate = 0.218;

}

否则if(unit> = 201&& unit< = 800){

rate = 0.345;

}

else {

rate = 0.421;

}

金额= horsePower * 0.746 * rate * numberHours;

金额= sc .nextInt();

System.out.println(支付金额=+金额);



什么我试过了:



i试图编译它,但我的计算中仍然有错误。你可以帮助我吗?

解决方案

该代码有很多问题,但你得到的错误是:

 amount = horsePower * 0.746 * rate * numberHours; 



金额是一个整数,但结果右边的表达式是一个double(因为浮点数据0.746和 rate 的存在意味着它都被提升到浮点,即使 horsePower numberHours 是整数 - 它不是浮点数必须被截断到一个整数,你的总是得到零作为结果)。

当你为一个整数分配一个双精度时,你会丢失信息,因为你必须扔掉小数点右边的任何东西。



编译器希望确定您知道自己在做什么,因此它会给您一个错误以确保。你可以这样做,但你必须先抛出它以避免错误:

 amount =(int)(horsePower * 0.746 * rate * numberHours); 



但是如前所述,如果您立即用用户输​​入覆盖该值,那就非常无关紧要了!

 amount = horsePower * 0.746 *率* numberHours; 
amount = sc.nextInt(); //< ----移动或移除此!
System.out.println(支付金额=+金额);





但是请帮自己一个忙停止使用魔术数字 - 它们不会使代码易于理解或维护。


Quote:

我试过编译它,但我的计算中仍然有错误。



你怎么知道计算中有错误?

 amount = horsePower *  0  746  * rate * numberHours;  //   1)在这里你做一些calcylation  
amount = sc.nextInt(); // 2)在这里用用户输入替换它
System.out.println(< span class =code-string> 支付金额= +金额); // 3)所以,在这里打印用户输入


if (unit >= 1 && unit <=200){
rate = 0.218;
}
else if (unit >= 201 && unit <= 800){
rate = 0.345;
}
else {
rate = 0.421;
}
amount = horsePower*0.746*rate*numberHours;
amount = sc.nextInt();
System.out.println ("amount to pay is =" + amount);

What I have tried:

i have tried to compile it and it still have error in my calculation. Can you help me ?

解决方案

There are many things wrong with that code, but the error you are getting is here:

amount = horsePower*0.746*rate*numberHours;


amount is an integer, but the result of the expression on the right is a double (because the presence of floating point data "0.746" and "rate" means that it all gets "promoted" to floating point even if "horsePower" and "numberHours" are integer - it it wasn't then the floating point would have to be truncated to an integer and your alway get a zero as the result).
When you assign a double to an integer you lose information because you have to "throw away" anything to the right of the decimal point.

The compiler wants to be sure that you know what you are doing so it gives you an error to make sure. You can do it, but you have to cast it first to avoid the error:

amount = (int) (horsePower * 0.746 * rate * numberHours);


But as has been mentioned, if you immediately overwrite the value with the user input it's pretty irrelevant!

amount = horsePower*0.746*rate*numberHours;
amount = sc.nextInt();  // <---- Move or remove this!
System.out.println ("amount to pay is =" + amount);



But do yourself a favour and stop using "magic numbers" - they don't make code at all easy to understand or maintain.


Quote:

i have tried to compile it and it still have error in my calculation.


How do you know that there is an error in calculation?

amount = horsePower*0.746*rate*numberHours;         // 1) here you do some calcylation
amount = sc.nextInt();                              // 2) here you replace it with user input
System.out.println ("amount to pay is =" + amount); // 3) so, here you print the user input


这篇关于在我的编码中有错误,这可能是从double到int的有损转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆