我是 Java 初学者,我正在尝试编写一个简单的 Java 程序,该程序根据初始存入金额、年数和利率来计算储蓄帐户中的金额。这是我的代码,它可以编译,但实际上并没有打印帐户中的金额(基本上,它完全忽略了我的第二种方法)。
import java.util.*;
class BankAccount {
public static Scanner input = new Scanner(System.in);
public static double dollars;
public static double years;
public static double annualRate;
public static double amountInAcc;
public static void main(String[] args) {
System.out.println("Enter the number of dollars.");
dollars = input.nextDouble();
System.out.println("Enter the number of years.");
years = input.nextDouble();
System.out.println("Enter the annual interest rate.");
annualRate= input.nextDouble();
}
public static void getDouble() {
amountInAcc = (dollars * Math.pow((1 + annualRate), years));
System.out.println("The amount of money in the account is " + amountInAcc);
}
}
我认为这是因为我没有在任何地方调用该方法,但我对如何/在哪里执行此操作有点困惑。
最佳答案
从扫描器
收到所有必需的输入后调用它。
// In main
System.out.println("Enter the number of dollars.");
dollars = input.nextDouble();
System.out.println("Enter the number of years.");
years = input.nextDouble();
System.out.println("Enter the annual interest rate.");
annualRate= input.nextDouble();
getDouble(); // Print out the account amount.
关于Java - 输出不打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110248/