嗨,我挖了一段時間,但無法找到與此主題相關的問題。我正在編寫一個程序,輸出一張表格,列出信用卡用戶的最低付款和餘額。問題發生在代碼的最後一部分,我必須在輸出之間放置空格,並將輸出格式化爲2位小數。格式化小數位格式化System.out.print Java
在此先感謝!
我的代碼:
public static void main(String[] args) {
double minPay = 0;
Scanner key = new Scanner(System.in);
System.out.printf("Please Enter your credit card balance: ");
double balance = key.nextDouble();
System.out.printf("Please Enter your monthly interest rate: ");
double rate = key.nextDouble();
System.out.printf("%-6s%-10s%-10s\n", "", "Min.Pmt", "New Balance");
System.out.printf("------------------------------\n");
for (int i=0; i<12; i++){
minPay = balance*0.03;
if (minPay<10){
minPay=10.00;
}
double add = (balance*(rate/100));
balance += add;
balance -= minPay;
System.out.printf("%-6s%-10.2f%-10.2f\n", i+1 + ".", "$" + minPay, "$" + balance);
我認爲'DecimalFormat'完全適合這裏:) –
...什麼是預期的輸出(舉一個例子),你會得到什麼呢? – chrylis