所以基本上,我的程序編譯正確並且正在工作。但是,當程序計算單個興趣時,它會顯示不正確的值。說它應該顯示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);
}
}
}
使用調試器查看正在發生的事情,或者如果您不熟悉使用調試器(更好地瞭解它),還可以在不同位置打印出值,看看它們是否符合您的期望。 – Kayaman
停止除以100吧?我認爲我們需要一些更多的信息,比如我們可以幫助你的信息。 – simonv
打印錯誤的號碼,是否所有的數字都會發生(複利,總利息,單利)?它似乎總是打印1/100它應該? –