输出格式化Java

输出格式化Java,java,formatting,printf,Java,Formatting,Printf,我的问题很简单,如何让我的输出格式化两个小数位的值。无论出于何种原因,我都无法输出小数点后两位的“当前余额”和“新余额”。单独使用时,它们工作正常,但一起使用时,会出现转换错误。这是我不知道的规则吗?如果可能的话,我想用一个动作来完成。这只是一个表面问题,当我取出格式化文件时,所有操作都可以正常执行 谢谢 public static void main(String[] args) { double min; int accNum; char accType;

我的问题很简单,如何让我的输出格式化两个小数位的值。无论出于何种原因,我都无法输出小数点后两位的“当前余额”和“新余额”。单独使用时,它们工作正常,但一起使用时,会出现转换错误。这是我不知道的规则吗?如果可能的话,我想用一个动作来完成。这只是一个表面问题,当我取出格式化文件时,所有操作都可以正常执行

谢谢

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);
publicstaticvoidmain(字符串[]args)
{
双分钟;
int accNum;
字符类型;
双bal;
双纽巴;
//用户输入
System.out.println(“请输入您的账号:”);
accNum=console.nextInt();
System.out.println();
System.out.println(“输入账户类型:s(储蓄)或c(支票)”;
accType=console.next().charAt(0);
System.out.println();
//储蓄与支票
开关(accType)
{
案例s:
案例S:
System.out.println(“输入当前余额:”);
bal=console.nextDouble();
System.out.println(“输入最小余额:”);
min=console.nextDouble();
System.out.println();
如果(bal=min)
newBal=bal+(bal*C_INT);
其他的
newBal=余额+(余额*C_INT_MAX);
System.out.printf(“帐户:”+accNum+“\n”
+帐户类型:正在检查“+”\n“+”当前余额:
+“$%.2f%n”,余额+“新余额:$%.2f%n”,新银行);

这是因为您将格式放在第一位,并且仅放在第一位

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

这与一小时前发布的问题/错误相同。

感谢您的帮助,我尝试了搜索,但没有找到我要找的内容。这仍然是编程和网站的一个绝妙之处。再次感谢您的洞察力。@DaneChristian这是阅读文档的好地方。