-3
我正在构建温度表,我不确定如何在我的程序的for循环中实现小数格式。我已经添加了十进制格式。下面是我的程序循环部分:如何把我的程序以正确的十进制格式使用十进制格式类?
DecimalFormat pattern = new DecimalFormat("##0.##");
String table = " Faren. Celsius Kelvin "; // Create the header
table += "\n------------------------"; // Add the heading underline
for (double i = start; i <= end ; i += step)
{
double c = ((5.0/9.0) * (i - 32));
double k = c + 273.15;
table += "\n | " + i + "\t | " +c + " | \t" + k + " |\n";
}
table += "\n------------------------";
System.out.println (table);
JOptionPane.showMessageDialog(null, (table));
你应该使用'pattern.format(c)'而不是'c'。 另请参阅方法['format(Object)']的javadoc(https://docs.oracle.com/javase/8/docs/api/java/text/Format.html#format-java.lang.Object- )。 –