2

问题是“转换='-'”。源代码在这里,我已经注释了“printf”以避免一些问题,总是用“print”来区分:这是一个用于计算年贷款的程序

import java.util.*;
public class Loan{
    public static void main(String args[]){
        final double MIN = 0.05;
        final double MAX = 0.08;
        final double ADD = 0.125;
        Scanner input = new Scanner(System.in);
        System.out.print("Loan Amount: ");
        double amount = input.nextDouble();
        System.out.print("Number of Years :");
        int years = input.nextInt();
        /*  
        for(double r = MIN; r < MAX;r = r + ADD){
          double R = Math.pow((1+r),years);
          double monthlyPayment = r * R * amount / 12/(R-1);
          double totalPayment = 12*monthlyPayment*years;
          System.out.printf("%-20s%-20s%-20\n","InterestRate","MonthlyPayment","TotalPayment");
          System.out.printf("%%-20f%-20.2f%-20.2f\n",r,monthlyPayment,totalPayment);
         }
         */     
     }
}
4

3 回答 3

14

您缺少格式说明符字符,请替换

System.out.printf("%-20s%-20s%-20", "InterestRate", ...)

System.out.printf("%-20s%-20s%-20s", "InterestRate", ...
                                 ^
于 2013-03-06T11:26:22.200 回答
1

使用下面的代码它会工作

System.out.printf("%-20s %-20s %20s %n","InterestRate","Monthly Payment","Total Payment\n");               
System.out.printf("%-20f %-20.2f %-20.2f\n",r,monthlyPayment,totalPayment);

让我知道是否还有其他需要

于 2013-03-06T11:39:15.097 回答
1

我猜你想要这个

System.out.printf("%-20s%-20s%-20s", "InterestRate",
        "MonthlyPayment", "TotalPayment\n");
System.out.printf("%-20.2f%-20.2f%-20.2f\n", r, monthlyPayment,
        totalPayment);
于 2013-03-06T11:29:44.887 回答