2016-08-28 151 views
-2

所以基本上,我的程序編譯正確並且正在工作。但是,當程序計算單個興趣時,它會顯示不正確的值。說它應該顯示470美元,而不是隻打印出4.7,對不起的解釋抱歉,任何人都可以弄清楚爲什麼會發生這種情況?編程新手,需要一點幫助

import java.io.*; 
import java.util.Scanner; 
import java.io.File; 
//import java.nio.file.*; 
//import java.nio.file.Paths; 
public class BankInterest { 

    public static void main (String [] args) throws IOException 
    { 

    /* TASK 1: Declare variables */ 

    Scanner user_input = new Scanner (System.in); 

    boolean exit; 
    int accountType; 
    double balance; 
    double principal; 
    // double userInterest; 
    double r; 
    double r2; 
    int year; 

    String commBank = ("commbank.txt"); 
    String westPac = ("westpac.txt"); 

    /*Check if the expected command line is provided */ 

    if (args.length < 1) { 
    /* Display the Usage */ 
    System.out.println("Usage: java BankInterest interestRateFileName"); 
    /* Programs quits with an error code */ 
    System.exit(-1); 
} 

    /* TASK 2: Read interest rates from a file */ 

    String filename = (args[0]); 
    Scanner textReader = new Scanner(new File(filename)); 
    r = textReader.nextDouble(); 
    r2 = textReader.nextDouble(); 
    System.out.println(r); 
    System.out.println(r2); 

    /* TASK 3: Take user input - Which Account */ 

    Scanner keyboard = new Scanner (System.in); 
    System.out.println("Which Account: "); 
    System.out.println("1 - Savings"); 
    System.out.println("2 - Term Deposits"); 

    accountType = keyboard.nextInt(); 

    if (accountType == 1) { 
     accountType = 1; 
} 
    else if (accountType == 2) { 
     accountType = 2; 
} 

    /* TASK 4: Take user input - Principal and Period */ 

    Scanner input = new Scanner (System.in); 
    System.out.println("Principal: "); 
    principal = keyboard.nextDouble(); 

    System.out.println("Years: "); 
    year = keyboard.nextInt(); 

    /* TASK 5: Calculate balance for the chosen account type */ 

    if (accountType == 1) { 
     double userBalance = principal * Math.pow((1 + r/100), year); 
     double userInterest = userBalance-principal; 
     System.out.println(""); 
     System.out.println("The Compound Interest is: " + userBalance); 
    System.out.println("The total amount of Interest earned:" + userInterest); 
    } 
    else if (accountType == 2) { 
     double userBalance = (principal * r2 * year)/100; 
     double userInterest = userBalance-principal; 
    System.out.println(""); 
     System.out.println("The Simple Interest is: " + userBalance); 
    System.out.println("The total amount of Interest earned:" + userInterest); 

    } 
} 
} 
+3

使用調試器查看正在發生的事情,或者如果您不熟悉使用調試器(更好地瞭解它),還可以在不同位置打印出值,看看它們是否符合您的期望。 – Kayaman

+2

停止除以100吧?我認爲我們需要一些更多的信息,比如我們可以幫助你的信息。 – simonv

+0

打印錯誤的號碼,是否所有的數字都會發生(複利,總利息,單利)?它似乎總是打印1/100它應該? –

回答

0

第一個公式是錯誤的。正確的是(假設r2代表%):

double userInterest = (principal * r2 * year)/100; 
double userBalance = principal + userInterest; 
+1

答案可能是正確的,但你需要解釋爲什麼它是錯誤的,並張貼正確的一個 – c0der

+0

老兄其簡單的公式我應該怎麼解釋SI =本金*利率*時間/ 100 – Ashish

+1

@Ashish,我相信你的公式是不正確或至少不是OP希望使用的那個。 –

0

我想這只是因爲你給的利率爲個百分點。我的意思是20%的利率,文件中的值是0.20。在你的計算中,你再次除以100。將您的興趣值更改爲20或在計算中將100除以100。