使用HTML表单(Java)中的变量执行计算时获取NaN结果
我试图使用通过HTML表单提交的值来计算每月的汽车贷款支付,这些值被传递到一个servlet,该servlet使用Java类来给出答案。每次我运行表单,结果都是NaN 表格:使用HTML表单(Java)中的变量执行计算时获取NaN结果,java,servlets,nan,Java,Servlets,Nan,我试图使用通过HTML表单提交的值来计算每月的汽车贷款支付,这些值被传递到一个servlet,该servlet使用Java类来给出答案。每次我运行表单,结果都是NaN 表格: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Car Loan</title> </head> <
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Car Loan</title>
</head>
<body>
<h1>Car Loan Calculator</h1>
<form method = "get" action = "CalcPayment">
<label> APR: </label><input type ="text" name ="apr"><br>
<label> Term (in years): </label><input type ="text" name
="term"><br>
<label> Principal: </label><input type ="text" name
="principal"><br>
<input type="submit" value ="Get Loan Payment">
</form>
<p>
Loan payment is: ${carLoan.getPayment()}
</p>
</body>
</html>
对象:
package pojos;
public class CarLoan {
private int apr, term;
private double principal;
public CarLoan (){
this.apr = 0;
this.term = 0;
this.principal = 0;
}
public CarLoan(int apr, int term, double principal) {
this.setApr(apr);
this.setTerm(term);
this.setPrincipal(principal);
}
public double getApr() {
return apr;
}
public void setApr(int apr) {
if(apr >= 0 && apr <= 30){
this.apr = apr;
}
else{
throw new IllegalArgumentException("APR must be between 0 and
30");
}
}
public double getTerm(){
return term;
}
public void setTerm(int term) {
if(term>=1 && term<=6){
this.term = term*12;
}
else{
throw new IllegalArgumentException("Loan term must be between 1
"
+ "and 6");
}
}
public double getPrincipal(){
return principal;
}
public void setPrincipal(double principal) {
if(principal>=0){
this.principal = principal;
}
else{
throw new IllegalArgumentException("Principal amount cannot be
+ "negative");
}
}
public double getPayment(){
double r = ((this.apr/100)/12);
double denominator = (Math.pow(1+r, this.term) - 1);
double numerator= (r * Math.pow(1 + r, this.term));
return (numerator/denominator);
}
@Override
public String toString() {
return String.format("rate= %d term=%d principal=%.2f", this.apr,
this.term, this.principal,+ this.getPayment());
}
}
封装pojo;
公共级卡拉酒店{
私营企业,任期;
私人双重委托人;
公共卡拉酒店(){
此值为0.apr=0;
该项=0;
这1.1=0;
}
公共CarLoan(整年4月,整年期限,双本金){
这是一个4月(4月);
本条第3款(期限);
这是一位校长(校长);
}
公共双getApr(){
返回四月;
}
公共无效设置apr(int apr){
如果(apr>=0&&apr=1&&term=0){
this.principal=principal;
}
否则{
抛出新的IllegalArgumentException(“本金金额不能为
+“否定”);
}
}
公共双倍支付(){
双r=((本年4月/100)/12);
双分母=(数学功率(1+r,本项)-1);
双分子=(r*Math.pow(1+r,这个项));
返回值(分子/分母);
}
@凌驾
公共字符串toString(){
返回String.format(“rate=%d term=%d principal=%.2f”,this.apr,
this.term,this.principay,+this.getPayment());
}
}
我认为问题出在getPayment()的最后一行。如果分母和分子都是0.0,您将得到NaN错误
您可以通过临时返回一个固定值来尝试,看看NaN错误是否仍然存在。如果您能告诉我们您在代码的哪一行获得NaN以及您当时使用的输入,我们将更容易为您提供帮助。
package pojos;
public class CarLoan {
private int apr, term;
private double principal;
public CarLoan (){
this.apr = 0;
this.term = 0;
this.principal = 0;
}
public CarLoan(int apr, int term, double principal) {
this.setApr(apr);
this.setTerm(term);
this.setPrincipal(principal);
}
public double getApr() {
return apr;
}
public void setApr(int apr) {
if(apr >= 0 && apr <= 30){
this.apr = apr;
}
else{
throw new IllegalArgumentException("APR must be between 0 and
30");
}
}
public double getTerm(){
return term;
}
public void setTerm(int term) {
if(term>=1 && term<=6){
this.term = term*12;
}
else{
throw new IllegalArgumentException("Loan term must be between 1
"
+ "and 6");
}
}
public double getPrincipal(){
return principal;
}
public void setPrincipal(double principal) {
if(principal>=0){
this.principal = principal;
}
else{
throw new IllegalArgumentException("Principal amount cannot be
+ "negative");
}
}
public double getPayment(){
double r = ((this.apr/100)/12);
double denominator = (Math.pow(1+r, this.term) - 1);
double numerator= (r * Math.pow(1 + r, this.term));
return (numerator/denominator);
}
@Override
public String toString() {
return String.format("rate= %d term=%d principal=%.2f", this.apr,
this.term, this.principal,+ this.getPayment());
}
}