gpt4 book ai didi

Java贷款计算器

转载 作者:行者123 更新时间:2023-11-30 04:53:19 27 4
gpt4 key购买 nike

我正在尝试编写一个贷款计算器。我好像遇到问题了我正在尝试从用户那里获取输入并验证输入。我知道我做错了,问题是我挠头不知道如何做正确。我在 d = getDouble(sc, Prompt); 上看到一条红线和 i = getInt(sc, 提示);据我所知,我的编码不正确。我只是不确定如何修复它。

我还必须验证 continue 语句,但我不确定最好的方法,最后讲师期望代码为 80 行或更少,我对 80 行的看法是正确的。我想我正在寻找一种更好的方法来做到这一点,但作为新手,我正在摸不着头脑,我希望有人能伸出援手。

一如既往,我非常感谢您的帮助。

   import java.util.Scanner;
import java.text.NumberFormat;

public class LoanCalculator
{
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
double d = 0.0;
boolean isValid = false;
while(isValid == false);
{
d = getDouble(sc, prompt);
if (d <= min)
{
System.out.println("Error! Number must be greater tha 0.0");
}
else if (d >= max)
{
System.out.println("Error number must be less than 1000000.0");
}
else
isValid = true;
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
{
int i = 0;
boolean isvalid = false;
while(isvalid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println("Error! Number must be more than 0");
else if (i >= max)
System.out.println("Error! Number must be less than 100");
else
isvalid = true;
}
}
public static void main(String[] args)
{
System.out.println("Welcome to the loan calculator");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0);
double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
int months = years * 12;

double monthlyPayment = loanAmount * interestRate/
(1 - 1/Math.pow(1 + interestRate, months));

NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(3);
System.out.println("RESULST");
System.out.println("Loan Amount" + currency.format(loanAmount));
System.out.println("Yearly interest rate: " + percent.format(interestRate));
System.out.println("Number of years: " + years);
System.out.println("Monthly payment: " + currency.format(monthlyPayment));

System.out.println();
System.out.println("Continue? (y/n): ");
choice =sc.next();
System.out.println();

}
}
}

最佳答案

您尚未实现 getDouble(Scanner,String) 和 getInt(Scanner,String),这就是您看到红线的原因。

因为你已经有一个扫描仪,并且提示字符串将其更改为这个

System.out.print(prompt);
d = sc.nextDouble();

对于整数

System.out.print(prompt);
i = sc.nextInt();

关于Java贷款计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9406410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com