我正在为一项作业编写一个程序。 在那里,我陷入了如何为我的程序添加日期和月份的困境。
我已经可以将简单利息转换为年,但不能转换为几个月和几天:
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/