出于某种原因,这种转换真的让我很头疼。我可以在纸上和头脑中进行转换,但是当我尝试用 Java 为我的家庭作业编写它时,它真的让我一团糟。任务是让用户输入一个以千克为单位的数字并编写一个程序,该程序显示一条线上有多少磅,另一条线上有多少盎司。一般来说,转换让我很困惑,但我不确定这是否正确。我需要在任何地方使用类型转换吗?
import acm.program.*;
public class KilogramsToPoundsAndOunces extends ConsoleProgram {
public void run(){
println("This programs converts kilograms into pounds and ounces.");
double kilo = readDouble("Enter the kilogram value: ");
double totalOunces = (kilo * POUNDS_PER_KILOGRAM) * OUNCES_PER_POUND;
int totalPounds = totalOunces % OUNCES_PER_POUND;
double leftOverOunces = totalOunces - (totalPounds * OUNCES_PER_POUND);
println(totalPounds + "lbs" + ".");
println(leftOverOunces + "ozs" + ".")
}
private static void POUNDS_PER_KILOGRAM = 2.2;
private static void OUNCES_PER_POUND = 16;
}