我正在尝试将一系列计算格式化为表格。到目前为止,我已经得到了字符串数据类型,但双数据类型似乎表现不同。运行程序时,我收到格式转换错误。我是java的新手,非常感谢任何帮助。我试图让它看起来像这样:
RATE YEAR AMOUNT ON DEPOSIT INTEREST
---- ---- ----------------- --------
0.07 1 1,070.00 70.00
2 1,144.90 144.90
3 1,225.04 225.04
4 1,310.80 310.80
5 1,402.55 402.55
6 1,500.73 500.73
7 1,605.78 605.78
8 1,718.19 718.19
9 1,838.46 838.46
10 1,967.15 967.15
这是我的代码:(我知道循环是关闭的,如果它看起来像上面那样。我只是想弄清楚如何格式化双打。)
import java.text.DecimalFormat;
public class interest
{
public static void main(String[] args)
{
DecimalFormat money = new DecimalFormat("$#,##0.00");
DecimalFormat percent = new DecimalFormat("0.00");
double interest = 0;
double principal = 1000;
double rate7 = 0.07;
double rate8 = 0.08;
double rate9 = 0.09;
double rate10 = 0.10;
double endYear = 0;
int yearCount = 1;
System.out.printf("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.printf("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
boolean firstTime =true;
while (yearCount <= 10)
{
if(firstTime)
{ //the issue is here
System.out.printf("1f%7d%20f%10f", "percent.format(rate7)", "yearCount","endYear", "interest");
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate7),yearCount);
System.out.println(money.format(endYear));
yearCount++;
interest = endYear - 1000;
System.out.println();
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate8));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate8),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate9));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate9),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
System.out.println();
}
}
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
System.out.println();
System.out.format("%1s%7s%20s%10s", "----", "----", "-----------------", "--------");
System.out.println();
yearCount = 1;
endYear = 0;
interest = 0;
firstTime = true;
while (yearCount <= 10)
{
if(firstTime)
{
System.out.println(percent.format(rate10));
firstTime = false;
}
else
{
endYear = principal * Math.pow((1 + rate10),yearCount);
yearCount++;
interest = endYear - 1000;
System.out.println(money.format(endYear));
System.out.println();
}
}
}
}
答案 0 :(得分:0)
替换
System.out.format("%1s%7s%20s%10s", "Rate", "Year", "Amount on Deposit" , "Interest");
与
System.out.printf("%s %7d %20f %10f", percent.format(rate7), yearCount, endYear, interest);
当你传递字符串而不是数字时。此外,对于第一个字符串元素,您没有指定'%s'。一旦你替换它,你的代码将正常工作