Java - 输出不打印?

标签 java output

我是 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/

相关文章:

java - Eclipse 不刷新包资源管理器 View 中的项目文件

java - Dropwizard 在有效 GET 上返回 400

python - return 语句的目的是什么?它与打印有何不同?

xslt - 为每个 xsl :text tag in the xml 设置禁用输出转义 ="yes"

linux - 每次返回新输出时将应用程序输出的最后一行重定向到文件

java - 如何在向下滚动时固定标题位置

Java运行JarInputStream

ruby-on-rails - 挂起时如何调试 `rails s`?

java - Spring 和 hibernate : form for object containing objects

PHP MySQL将html输出三乘三排列,按组的第一个字母排序