2016-02-21 79 views
-4

這個程序計算前6個月的利息給定量,它不工作的錯誤在哪裏?程序來計算利息

import java.util.Scanner; 

public class BalAfter6Months{ 

    public static void main(String []args){ 

    int counter=1; 
    double interest,SavBal,total,amount; 
    Scanner sc = new Scanner (System.in); 
    System.out.print("Enter the monthly saving amount: "); 
    amount = sc.nextDouble(); 

    SavBal = amount; 

    while (counter<7) { 
     interest = amount * 0.00417; 

     total = interest + SavBal; 

     amount = amount + SavBal; 

     counter++; 
    } 

     System.out.print(total); 

    } 

} 
+1

你應該告訴我們你得到了什麼以及你的期望。 – Arc676

+5

歡迎來到StackOverflow。請訪問[幫助]並閱讀[問]。形式問題「這裏是我的代碼,它不起作用,請告訴我什麼是錯誤的」在這裏通常被認爲是脫離主題。你必須解釋什麼不起作用(顯示預期的和實際的輸出)並解釋你已經完成的調試。 –

+0

那麼如果你的意思是它不能編譯,試着初始化總數' – MartinS

回答

0

這避免了不必要的變量,IMO要簡單得多。

import java.util.Scanner; 

public class BalAfter6Months{ 

    public static void main(String []args){ 

     double interest, balance; 

     Scanner sc = new Scanner (System.in); 
     System.out.print("Enter the monthly saving amount: "); 

     balance = sc.nextDouble(); 

     int count = 1 
     while (counter < 7) { 
      interest = balance * 0.00417; 
      balance += interest 
      counter++; 
     } 

     System.out.print(total); 

    } 

} 
0

您已編碼正確。您只需初始化total變量以避免編譯器錯誤。

/* Initialize Total Variable */ 
double interest,SavBal,total = 0,amount; 

輸入:

100 

輸出:

102.502