Java 作业:使用RNG将英尺转换为英寸
“编写一个随机生成若干总英寸的程序。然后将总英寸转换为英尺和英寸。” 我需要以以下格式进行输出: 14英寸是1英尺,2英寸 我已经有了这个起始代码:Java 作业:使用RNG将英尺转换为英寸,java,converters,Java,Converters,“编写一个随机生成若干总英寸的程序。然后将总英寸转换为英尺和英寸。” 我需要以以下格式进行输出: 14英寸是1英尺,2英寸 我已经有了这个起始代码: public class InchesToFeet { public static void main(String[] args) { convert(); // convert feet to inches and output } // This method generate a random nu
public class InchesToFeet {
public static void main(String[] args) {
convert(); // convert feet to inches and output
}
// This method generate a random number of total inches.
// It then converts to feet and inches and outputs the answer.
public static void convert() {
// randomly picks a number between 1-200
int totalInches = (int)(Math.random()*200 + 1);
// Convert to feet and inches.
// ex. if totalInches is 38, the output would be: 38 inches is 3 feet, and 2 inches
// ADD CODE BELOW
}
}
我读了教授指定的那一章,但我真的不知道该怎么做。谢谢 为了实现您的目标,您需要首先将
totalInches
除以转换因子(12)以查看它包含多少英尺,然后使用模运算器(%)获得英寸数,如下所示
System.out.println(totalInches+“inches”是“+(int)(totalInches/12)+”英尺和“+totalInches%12+“inches”)代码>
不过,在这里粘贴作业不是一个好主意