简体   繁体   English

如何为简单的利率计算添加月和日 (Java)

[英]How to add months and days for a simple interest-rate calculation (Java)

I am working on a program for an assignment.我正在制定一项任务计划。 There I got stuck on how to add the days and months for my program.在那里我陷入了如何为我的程序添加天数和月数的问题。

I can already convert the simple interest into years , but not for months and days:我已经可以将简单的兴趣转换为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);   
    }    
  }

Just ask them separatly then compute the global time分别询问他们然后计算全球时间

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

I'd suggest :我建议:

  • don't define variable before using it if you don't need to do so如果不需要,请不要在使用之前定义变量
  • use nextLine and parse what you need, you'll avoid suprise with return char使用 nextLine 并解析您需要的内容,您将避免使用 return char 感到惊讶
  • as Java convention, use lowerCamelCase to name your variables作为 Java 约定,使用 lowerCamelCase 来命名您的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM