日期:2014-05-20  浏览次数:20676 次

这两个字符表示什么意思
public class Interest 
{
public static void main(String args[])
{
double amount;
double principle = 1000.0;
double rate = 0.05;
System.out.printf("%s%20s\n","Year","Amount on deposit");
for(int year = 1; year <= 10;year++)
{
amount = principle * Math.pow( 1.0 + rate ,year);
System.out.printf("%4d%,20.2f\n",year,amount);
}

}
}
"%s%20s\n"这个字符中的20表示什么意思?
"%4d%,20.2f\n"这个字符转换是什么意思?20.2f是什么意思?

------解决方案--------------------
java5中的推出的类似C语言printf风格的格式化输出
------解决方案--------------------
1楼正解
20 宽度
.2 精度
还可以写成
Java code

System.out.format("%4d%,20.2f\n",year,amount);
//或
System.out.format(String.format("%4d%,20.2f\n",year,amount));