下面的等式應該輸出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);
錯誤是您正在使用整數除法。 – Tunaki
是的,只需聲明所有變量爲「double」而非「int」。 –
打印出「twos」而不是「twosFloat」。 –