【问题标题】:How to add months and days for a simple interest-rate calculation (Java)如何为简单的利率计算添加月份和日期(Java)
【发布时间】:2019-11-27 20:39:44
【问题描述】:

我正在为一项任务编写一个程序。 在那里,我陷入了如何为我的程序添加天和月的问题。

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

  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);   
    }    
  }

【问题讨论】:

  • 你试过什么?你有没有为此制定一个总体计划?
  • daysmonths 的用途是什么?如果它们要受到nominal interest rate每日或每月计算,您是否需要数学公式,还是您的问题是将公式带入代码?
  • 我应该编写一个简单的兴趣程序,显示作业的年日和月,没有提到在代码中使用公式
  • 嘿!这是您还是您的一位同事在问类似的问题:stackoverflow.com/questions/59079261/…

标签: java finance calculation


【解决方案1】:

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

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 并解析您需要的内容,您将避免使用 return char 感到惊讶
  • 按照 Java 约定,使用 lowerCamelCase 命名变量

【讨论】:

猜你喜欢
  • 2010-10-13
  • 2011-08-20
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多