以下是StackTrace
:
`adding $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Formatter.java:2487)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at accounttest.main(accounttest.java:18)`
以下是我要说的代码:
import java.util.*;
import java.io.*;
public class accounttest{
public static void main(String[] args) {
account account1 = new account(50.00);
account account2 = new account(-7.50);
System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());
Scanner input = new Scanner(System.in);
double depositAmount;
System.out.printf("Enter deposit amount for account 1\n>>");
depositAmount = input.nextDouble();
System.out.printf("\nadding $%.2f to account 1 balance\n\n");
account1.credit(depositAmount);
//displaying the current amount in both of the accounts
System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());
System.out.printf("Enter deposit amount for account 2\n>>");
depositAmount = input.nextDouble();
System.out.printf("\nadding $%.2f rupees to account 2 balance\n\n");
account2.credit(depositAmount);
System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());
}
}
答案 0 :(得分:2)
如堆栈跟踪所示,添加一个格式参数,因为您已经在其他语句中已经完成了
System.out.printf("\nadding $%.2f to account 1 balance%n", depositAmount);
^