输出格式化Java

时间:2016-02-08 23:52:17

标签: java formatting printf

我的问题很简单,如何让我的输出格式化两个小数位的两个值。无论出于何种原因,我无法输出“当前余额”& “新余额”,小数点后两位。他们分别工作正常,但在一起我得到转换错误。这是我不知道的规则吗?如果可能的话,我想用一个动作来做这件事。这只是一个美化问题,当我取出格式时,所有操作都能正常运行。

感谢,

public static void main(String[] args) 
{
    double min;
    int accNum;
    char accType;
    double bal;
    double newBal;

    //user inputs
    System.out.println("Please enter your account number: ");
    accNum = console.nextInt();
    System.out.println();

    System.out.println("Enter the account type: s(savings) or c(checking)");
    accType = console.next().charAt(0);
    System.out.println();

    //savings v checking
    switch (accType)
    {
        case 's':
        case 'S':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            { 
                newBal = bal - S_MIN_FEE;
                System.out.println("Insufficient Funds (-$10.00)");
            }
            else
                newBal = bal + (bal * S_INT);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Savings" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f", newBal);
        case 'c':
        case 'C':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            {
                newBal = bal - C_MIN_FEE;
                System.out.println("Insufficent Funds (-$25.00)");
            }
            else if (bal < C_BAL_MAX && bal >= min)
                newBal = bal + (bal * C_INT);
            else
                newBal = bal + (bal * C_INT_MAX);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Checking" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);

1 个答案:

答案 0 :(得分:0)

那是因为你把格式放在第一位,而且只放在第一位。

System.out.printf("%.2f %.2f%n", bal, newBal);

这与一小时前发布的问题/错误相同。 java.util.IllegalFormatConversionException: f != java.lang.String Error