我对Java编程很陌生,在任何地方都找不到解决我问题的答案。基本上,我已经成功创建了一个程序,该程序可以构建摄氏到华氏转换和华氏到摄氏转换的图表,但是在数字9之后,我的循环打印语句未正确对齐,类似于:
9.0 48.2 40.0 4.44
10.0 50.0 41.0 5.0
我被要求使用两个单独的方法来计算转换,然后在main方法中调用它们。这是我引用的带有println语句的主要方法:
public static void main(String[]args){
double celsius = 1;
double fahrenheit = 32;
while(celsius <= 50 && fahrenheit <= 120){
double toFarhenheit = celsiusToFahrenheit(celsius);
double toCelsius = fahrenheitToCelsius(fahrenheit);
DecimalFormat fardec = new DecimalFormat("#.##");
toFarhenheit = Double.valueOf(fardec.format(toFarhenheit));
DecimalFormat celsdec = new DecimalFormat("#.##");
toCelsius = Double.valueOf(celsdec.format(toCelsius));
System.out.println(celsius + " " + toFarhenheit + " " + fahrenheit +
" " +toCelsius);
celsius++;
fahrenheit++;
}
}
长话短说,是否有将printf与这种长打印语句一起使用,以便数字彼此对齐?
过去,我曾使用printf%3d和%5d等将整数对齐,但是,对于这种特定的print语句,我根本无法使用它。
任何想法和/或帮助将不胜感激。
使用\ t格式化它们,因为这会均匀地隔开它们。
System.out.println(celsius + "\t" + toFarhenheit + "\t" + fahrenheit +
"\t" +toCelsius);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句