java - 如何为简单的利率计算添加月份和日期(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);
}
}
解决方案
只需分别询问他们,然后计算全球时间
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 命名变量
推荐阅读
- javascript - 什么是 firebase.database()
- mysql - PHPMyAdmin 崩溃
- corda - 需要为每个构建生成节点信息文件,即使 node.conf 保持不变
- dart - Dart StreamController 有两个参数
- javascript - 模态表单上的 jQuery submit() vc click()
- kotlin - Kotlin 的作用域——暴露单一入口点
- asp.net-core - Cookie 忽略 DotNet Core 中的 CORS 设置
- ckeditor - Ckeditor 3 - 选择工具栏按钮时单击插入元素
- wpf - WPF 中的复杂标题样式
- c# - 在树视图中更改节点颜色