线程“main”中的异常java.util.UnknownFormatConversionException:Conversion =' - '

时间:2013-03-06 11:21:19

标签: java implicit-conversion

问题是“转换=' - '”。 源代码在这里,我已经评论了“printf”以避免一些问题,总是由“打印”来解决:这是一个用于计算年度贷款的程序

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);
         }
         */     
     }
}

3 个答案:

答案 0 :(得分:11)

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

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

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

答案 1 :(得分:1)

我猜你想要这个,

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

答案 2 :(得分: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);

如果需要其他任何内容,请告诉我