簡體   English   中英

輸出格式化Java

[英]output formatting Java

我的問題很簡單,我如何獲取輸出以格式化兩個帶有兩位小數位的值。 無論出於什么原因,我都無法輸出帶有兩個小數位的“當前余額”和“新余額”。 他們分別工作正常,但當我在一起時遇到轉換錯誤。 這是我不知道的規則嗎? 如果可能的話,我想采取一項行動。 這只是一個表面問題,當我取出格式化文件時,所有操作都執行良好。

謝謝,

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);

這是因為您將格式放在第一位,並且僅放在第一位。

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

這與一個小時前發布的問題/錯誤相同。 java.util.IllegalFormatConversionException:f!= java.lang.String錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM