简体   繁体   English

Java数学逻辑错误

[英]Java math logic error

I have the following code in my project: 我的项目中包含以下代码:

  int percent = 2;
  int count = 10;
  int percentagefill = (percent/10)*count; 
  System.out.println(percentagefill);

Basically what is happening is that, I'm setting two variables, percent and count. 基本上发生的是,我设置了两个变量,百分比和计数。 I then calculate the percentage fill. 然后,我计算百分比填充。 For some strange reason the percentage fill is resulting in a 0, when in this case it should be 2. Any ideas why? 出于某种奇怪的原因,百分比填充导致结果为0,在这种情况下应为2。 Thanks in advance. 提前致谢。

int divided by int will still result in int . int除以int仍将int In this case: 在这种情况下:

(percent/10)*count
= (2/10)*10
= (0) * 10 <-- 0.2 is rounded down to 0
= 0

You can read this question for reference. 您可以阅读此问题以供参考。 Also, here's the Java spec where is says that integer division is rounded towards 0. As for the fix, as long as floating point precision does not become an issue , just use double as PaulP.RO said. 另外,这是Java规范 ,其中整数整数被舍入为0。至于解决方法,只要浮点精度不成为问题 ,就可以像PaulP.RO所说的那样使用double

You could divide by 10.0 , or change int percent to double percent in order to force a conversion to double . 您可以将其除以10.0 ,或将int percent更改为double percent ,以强制将转换转换为double Otherwise, you are getting integer division, which truncates off the decimal part. 否则,您将获得整数除法,该除法将舍去小数部分。

Here is a relevant question: "Java Integer Division, How do you produce a double ?" 这是一个相关的问题: “ Java整数部分,您如何产生一个double ?”

如果您确实需要将结果设为int ,则可以在除法之前进行乘法运算,以避免整数除法为零。

int percentagefill = (percent*count)/10;

Change this line: 更改此行:

int percentagefill = (percent/10)*count; 

To: 至:

double percentagefill = (percent/10.0)*count;

This will use floating point arithmetic (because of the 10.0 instead of 10), and store the result in a double (which has precision past the decimal point unlike an int ). 这将使用浮点算术(因为是10.0而不是10),并将结果存储在double (与int不同,其精度超过了小数点)。

As others have mentioned, you're losing precision with integer division 正如其他人提到的那样,整数除法会损失精度

The solution depends on your needs: if your result needs to be an integer anyway, multiply first: 解决方案取决于您的需要:如果您的结果仍然需要为整数,请先乘以:

int percentagefill = (percent*count)/10;

Could be "good enough" for you (you'll get the correct answer rounded down). 可能对您“足够好”(您将获得正确的答案四舍五入)。

If you need to be able to get fractional answers, you need to convert things to floating point types: 如果需要获得分数答案,则需要将其转换为浮点类型:

double percentagefill = (percent/10.0)*count;
//                                 ^ the .0 makes this a double,
//                                forcing the division to be a
//                                floating-point operation.

It's easy to fix: 很容易解决:

  int percent = 2;
  int count = 10;
  double percentagefill = (percent/10.0)*count; 
  System.out.println(percentagefill);

Dave Newton, your answer should have been an answer. 戴夫牛顿,您的答案应该是答案。 :) :)

The integer 2 / the integer 10 = 0. The integer 0 * the integer 10 = 0. 整数2 /整数10 =0。整数0 *整数10 = 0。

You will need a float or double data type . 您将需要一个float或double 数据类型 While working with information, always be weary of chances for the interpreter to make data type assumptions and "casts". 在处理信息时,始终要让解释器做出数据类型假设和“转换”的机会感到厌倦。 println takes an intefer and casts to a string for display is one example. 一个例子就是println接受一个intefer并转换为字符串进行显示。

Most of my work is in php and when working with values, 0, NULL, ERROR can all be different things and can yield unexpected results. 我的大部分工作是在php中进行的,当使用值时,0,NULL,ERROR可能都是不同的东西,并且会产生意想不到的结果。 Sometimes you may need to explicitly cast a variable to a different data type to get the intended results. 有时,您可能需要将变量显式转换为其他数据类型才能获得预期的结果。

This is so due to the fact that you are using the integer data type when you should be using a floating-point data type such as double. 之所以如此,是因为您在应使用浮点数据类型(例如double)时使用整数数据类型。 This code should result in 2.0: 此代码应为2.0:

double percent = 2;
double count = 10;
double percentagefill = (percent/10)*count; 
System.out.println(percentagefill);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM