java - 如何添加月份和日期进行简单的利率计算 (Java)

标签 java finance calculation

我正在为一项作业编写一个程序。 在那里,我陷入了如何为我的程序添加日期和月份的困境。

我已经可以将简单利息转换为,但不能转换为几个月和几天:

  import java.util.Scanner;

  public class SimpleInterest {

    public static void main(String[] args) {
        double PAmount, ROI, TimePeriod, simpleInterset;
        Scanner scanner = new Scanner(System.in);

        System.out.print(" Please Enter the Principal Amount : ");
        PAmount = scanner.nextDouble();

        System.out.print(" Please Enter the Rate Of Interest : ");
        ROI = scanner.nextDouble();

        System.out.print(" Please Enter the Time Period in Years : ");
        TimePeriod = scanner.nextDouble();

        simpleInterset = (PAmount * ROI * TimePeriod) / 100;

        System.out.println("\n The Simple Interest for Principal Amount " + PAmount + " is = " + 
        simpleInterset);   
    }    
  }

最佳答案

只需分别询问他们,然后计算全局时间

System.out.print(" Please Enter the Principal Amount : ");
double pAmount = Double.parseDouble(scanner.nextLine());

System.out.print(" Please Enter the Rate Of Interest : ");
double rOI = Double.parseDouble(scanner.nextLine());

System.out.print(" Please Enter the Time Period in Years : ");
double years = Double.parseDouble(scanner.nextLine());
System.out.print("And months : ");
double months = Double.parseDouble(scanner.nextLine());
System.out.print("And days");
double days = Double.parseDouble(scanner.nextLine());

double timePeriod = years * months / 12 + days / 365;
double simpleInterset = (pAmount * rOI * timePeriod) / 100;

System.out.println("\n The Simple Interest for Principal Amount " + pAmount + " is = " + simpleInterset);

我建议:

  • 如果不需要,请不要在使用变量之前定义它
  • 使用 nextLine 并解析您需要的内容,您将避免返回字符带来的意外
  • 按照 Java 约定,使用小驼峰命名法来命名变量

关于java - 如何添加月份和日期进行简单的利率计算 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078106/

相关文章:

java - 正则表达式不匹配

java - 如何查找 MySQL 转储文件中存在的数据库名称?

r - 计算 R growth of growth 中的 Credit Impulse

java - 在计算 1+2+3+4...+n 时需要帮助吗?

java - 在 Java 中使用带有 for 循环的数组进行计算

java - 使用静态的 Selenium 框架

java - 在 Node 中加载 Java JNA 原生库

C++ 计算未以正确格式打印

google-api - Google Finance api 出现 403 错误

java - 计算Java的 future 投资额