我正在尝试编写一个将公斤转换为磅和盎司的程序。如果用户输入 100 公斤,我期望的结果是 220 磅和 7.4 盎司。
我得到了正确的磅值,但我的问题是得到了正确的盎司值。我不知道我错过了什么。此外,当我计算盎司值时,我如何向程序指定我只想要百分之一的答案。例如我只想要 7.4 盎司而不是 7.4353?
import acm.program.*;
public class KilogramsToPoundsAndOunces extends ConsoleProgram {
public void run() {
println("This program converts Kilograms into Pounds and Ounces.");
int kilo = readInt("please enter a number in kilograms: ");
double lbs = kilo * POUNDS_PER_KILOGRAM;
double oz = lbs * OUNCES_PER_POUND;
double endPounds = (int) oz / OUNCES_PER_POUND;
double endOunces = oz - (endPounds * OUNCES_PER_POUND);
println( endPounds + " lbs " + endOunces + "ozs");
}
private static final double POUNDS_PER_KILOGRAM = 2.2;
private static final int OUNCES_PER_POUND = 16;
}