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

梦想与她 提交于 2019-12-11 16:24:58

问题


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:

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

回答1:


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
  • as Java convention, use lowerCamelCase to name your variables


来源:https://stackoverflow.com/questions/59078106/how-to-add-months-and-days-for-a-simple-interest-rate-calculation-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!