是的,我以前用相同的代码问过问题,但出现了更多我似乎无法解决的问题,开始复利公式正在打印出奇怪数量的代码。还有 System.out.println(compound); 在主要方法中不起作用,因为“化合物无法解析为变量错误)所以我无法打印它。
import java.io.*;
class cexample {
public static double squared(double x){
return x*x;
}
public static void main(String[] args) throws IOException {
System.out.println(balance(0.0, 0.0,1.0));
System.out.println(balance);
}
static public double balance(double principal, double rate, double years) throws IOException{
double amount = 0;
String input;
String input2;
years = 1;
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
System.out.print("How much would you like to take out? ");
input = myInput.readLine ();
principal = Double.parseDouble (input);
System.out.print("Enter the interest rate: ");
input2 = myInput.readLine ();
rate = Double.parseDouble (input2);
System.out.println("Now, enter the amount of years (1-10)");
input = myInput.readLine ();
years = Double.parseDouble (input);
for (int i = 0; i < years; i++) {
// equation, M = P *( 1+ rate) * years^2
amount = principal * (1 + rate) *( squared(years));
//amount += principal;
}
System.out.println("The amount is " + amount);
return amount; //- principal;
}
}