首页 > 解决方案 > 如何为简单的利率计算添加月份和日期(Java)

问题描述

我正在为一项任务制定一个计划。在那里,我陷入了如何为我的程序添加日期和月份的问题。

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

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

标签: javafinancecalculation

解决方案


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

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 命名变量

推荐阅读