import java.util.Scanner;
public static void main(String[] args)
{
//Interest rate,number of years,loan amount; display the monthly total payments
Scanner input = new Scanner(System.in);
//Ask for interest rate
System.out.println("Enter your interest rate : ");
//Get interest rate
double interestRate = input.nextDouble();
//Ask number of years
System.out.println("Enter the number of years for this loan : ");
//Get number of years
double numberOfYears = input.nextDouble();
//Ask for loan amount
System.out.println("Enter your loan amount : ");
//Get loan amount
double loanAmount = input.nextDouble();
//Monthly interest rate
double monthlyInterestRate = interestRate / 12;
//Monthly payment
double monthlyPayment = (loanAmount * monthlyInterestRate) /
1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears*12)
;
//Total payment
System.out.println("total payment is : " + monthlyPayment * 12 * numberOfYears);
}
当我输入利率作为第一笔输入时,例如2.5
或2.0
,它给出了
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Sal.main(Sal.java:17)
我输入了double变量,它想要整数。我不明白是的,我确实导入了Scanner并输入了我的课程。我一周前买了电脑。相同的代码应该可以正常工作,因为它来自我的练习本。