Java 如何获得正确的输出?
所以对于这一点,我似乎只得到了输出的美元数量的“无穷大”。我想这和数学有关。(不要介意那些草率的代码,我被告知要在不做太多修改的情况下对其进行调试)这是我似乎遇到的唯一问题。除此之外,其他一切都很好Java 如何获得正确的输出?,java,math,Java,Math,所以对于这一点,我似乎只得到了输出的美元数量的“无穷大”。我想这和数学有关。(不要介意那些草率的代码,我被告知要在不做太多修改的情况下对其进行调试)这是我似乎遇到的唯一问题。除此之外,其他一切都很好 /* Chapter 3 Debugging Assignment *Programmer: *Date: *Program Name: Bert,java *Purpose: */ import java.util.Scanner; public class bert {
/* Chapter 3 Debugging Assignment
*Programmer:
*Date:
*Program Name: Bert,java
*Purpose:
*/
import java.util.Scanner;
public class bert {
public static void main(String[] args) {
//Declaring Variables
int price, downPayment, tradeIn, months, loanAmt, interest;
double annualInterest, payment;
String custName;
Scanner reader = new Scanner(System.in);
//Get Input from User
System.out.println("What is your first name? ");
custName = reader.next();
System.out.print("What is the price of the car? ");
price = reader.nextInt();
System.out.print("What is the downpayment? ");
downPayment = reader.nextInt();
System.out.print("What is the trade-in value? ");
tradeIn = reader.nextInt();
System.out.print("For how many months is the loan? ");
months = reader.nextInt();
System.out.print("What is the decimal interest rate? ");
annualInterest = reader.nextDouble();
//Output
calculatePayment(price, downPayment, tradeIn, (int) annualInterest, months);
}
public static void calculatePayment(int price, int downPayment, int tradeIn,
int months, double annualInterest) {
double interest;
int loanAmt;
double payment;
//Calculations
interest = (double) annualInterest/ (double) 12 ;
loanAmt = (int) (price - downPayment - tradeIn);
payment = loanAmt / ((1.0 / interest) - (1.0 / (interest * Math.pow(1.0, annualInterest))));
String custName = "Any Name";
System.out.print("The monthly payment for ");
System.out.print(custName + " is $ ");
System.out.println((double) payment);
return;
}
}
calculatePayment的参数不匹配
计算付款(价格、首付款、折价,(国际)年利息、月);应
计算付款(价格、首期付款、折价,月,年利息)
你的数学很差。
math.pow(1.0,annualInterest)总是
所以你有1/利息-1/利息,它是0。然后你有loanAmt/0.0==>无穷大
回去检查你的付款计算@MadProgrammer实际上他没有。下一个电话将跳过换行符。告诉我@panzo,你为什么要将利率(转换为
int
,因此小数部分被截断)作为月数,将月数作为利率?@immibis你知道,我真的很讨厌扫描器
@immibis哪一行?很抱歉,我对编码还不熟悉。@panzo cliff2310说它已经改变了这一点,但它仍然可以无限运行,这是数学问题还是代码本身的问题。