简单的等式没有返回正确的值

时间:2015-10-04 20:24:17

标签: java

下面的等式应该输出twosFloat为1,但它返回.8任何人都可以发现我的错误吗?

int weight = 50;
int barWeight = 45;

int weightWithoutBarWeight = weight - barWeight;

int fortyFives = (weightWithoutBarWeight / 2) / 45;
int thirtyFives = ((weightWithoutBarWeight / 2) - (fortyFives * 45)) / 35;
int twentyFives = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35)) / 25;
int tens = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25)) / 10;
int fives = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10)) / 5;
double twosFloat = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10) - (fives * 5)) / 2.5;
int twos = (int)twosFloat;

System.out.println(twosFloat);

1 个答案:

答案 0 :(得分:1)

您需要做的就是将每个等式的一些数字转换为浮点数或双精度数。符号" f"将整数转换为浮点数。

int weight = 50;
int barWeight = 45;

int weightWithoutBarWeight = weight - barWeight;

float fortyFives = (weightWithoutBarWeight / 2f) / 45f;
float thirtyFives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45)) / 35f;
float twentyFives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35)) / 25;
float tens = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25)) / 10f;
float fives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10)) / 5f;
double twosFloat = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10) - (fives * 5)) / 2.5f;
int twos = (int)twosFloat;

System.out.println(twosFloat);