如何使printf正确输出printf代码中没有错误的方法。老师没有仔细阅读格式只是一个简单的例子。
public static void printReportHeadings(int [] employeeId, int [] dependents, double [] hours, double [] payRate )
{
System.out.println(" ABC PayRoll System ");
System.out.println(" EmployeeId " + " Gross Pay " + " Federal Tax " + " State Tax " + " Net Pay ");
for (int i = 0; i <7;i++)
{
float gross;
double federal =0.0;
double state = 0.0;
double net = 0.0;
gross = (float)(hours[i]*payRate[i]);
federal = .2 * (gross-(dependents[i]*38.46));
state = .032*gross;
net = gross - (federal+state);
System.out.printf("%-15d %.01f % 15f%n", employeeId[i], gross, federal , state, net) ;
}
答案 0 :(得分:0)
你有三个占位符和五个你试图绑定到这些占位符的东西。
观察:
System.out.printf("%-15d %.01f % 15f%n", employeeId[i], gross, federal, state, net);
%-15d
,%.01f
和%15f
都是整数的占位符,分别是两个浮点数。其他两个 - state
和net
- 根本就没有占位符。
为浮点类型添加几个占位符将解决您的问题:
System.out.printf("%-15d %.01f %15f %10f %10f%n", employeeId[i], gross, federal, state, net);