java - 创建一个计算利息并显示 10 年利息成本的表格

标签 java eclipse

我对该行的兴趣类别是“6.5”,而它应该是“65”。这些数字每年都保持不变,而它们应该每年更新。另外,我似乎不知道如何在第二年更新我的新初始余额。我所说的更新是指第二年的新初始余额应该是第一年的期末余额,依此类推。

我的第二个问题是我的 return 语句 calcInterest();。我无法将结果转换为其他方法。感谢您的帮助!

import java.util.*;
import java.text.*;

public class Interest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printIntro(); // Inform user what program will compute
        Scanner console = new Scanner(System.in);
        System.out.print("What is the deposit balance ?: ");
        double balance = console.nextDouble();
        System.out.println(balance);
        System.out.print("What is the interest rate ?: ");

        double rate = console.nextDouble();
        System.out.println(rate);
        System.out.print("How many years?: ");
        int years = console.nextInt();
        System.out.println(years);
        printTable(years, balance, rate);
    }


    public static void printIntro() {
        System.out.println("This program will calculate interest on a deposit of your choosing over a specified period.");
    }

    public static void printTable(int numRows, double balance, double rate) {
        System.out.println("Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance");
        System.out.println("----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------");
        for (int i = 1; i <= numRows; i++) {
            printRow(i, balance, rate);
        }
    }

    public static void printRow(int rowNum, double balance, double interest) {
        System.out.println(rowNum + "\t" + balance + "\t" + "\t" + interest + "\t" + "\t" + (balance + interest));
        balance = (balance + interest);
    }

    public static double calcInterest(double balance, double rate) {
        double interest = balance * (rate / 100);
        return interest;
    }
}

最佳答案

我认为你的问题不小;我认为你有一个大问题:

您的应用程序中没有隐式状态,因此您的计算被破坏。

(此外,您甚至没有使用上一个方法。)

这更像是一个架构问题,因此我不会发布完整的解决方案,而是仅重点介绍此方法。

public static void printRow(int rowNum, double balance, double interest) {
    System.out.println(rowNum + "\t" + balance + "\t" + "\t" + interest + "\t" + "\t" + (balance + interest));
    balance = (balance + interest);
}

Java 是,并且永远是,pass-by-value 。这对您的应用程序意味着诸如 balance = (balance + interest) 之类的操作永远不会生效,因为更改仅在您的 printRow 范围内完成。方法。这意味着您的值(value)观将保持不变,即使这不是您想要做的。

这里可能的修复是:

  • balance 引入静态变量
  • 引入一个字段 balance

我想将其作为读者的练习,因为对于您来说,自己解决这些问题并探索什么适合您的应用程序非常重要。我和其他专业人士可能有解决问题的方法,但重要的是你也能得出自己的结论。

关于java - 创建一个计算利息并显示 10 年利息成本的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217131/

相关文章:

java - 哪个类提供了 getRequestDispatcher() 方法的实现

java - 如何为 json 字符串编写通用的 getObject() 方法?

java - 具有本地数据库的小型 Java 应用程序?使用哪个数据库?

c++ - Eclipse 运行-->断点类型-->无可用

java - 我的程序在 Eclipse 中运行,但不是作为可执行 jar 运行。 NoClassDefFoundError、ClassNotFoundException

android - NDK_ROOT 未定义。请在您的环境中定义 NDK_ROOT

c - 在 C 中读取 PGM 图像(未正确读取文件)

java - 用于验证媒体播放器的android单元测试

java - 在 Java 中验证直径 URI 的方法?

java - 包括不同操作系统中java项目编译的库文件