我正在为一项作业编写程序。
在那里,我陷入了如何为程序添加日期和月份的问题。
我已经可以将简单的利息转换成年,但是不能转换成几个月和几天:
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命名变量
关于java - 如何添加月份和日期以进行简单的利率计算(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078106/