import java.util.Scanner;
import java.text.DecimalFormat;
public class Assignemt7_2
{
public static void main (String [] args)
{
//Create a Scanner
Scanner input = new Scanner (System.in);
//Prompt the user to enter the amout of loan borrowed
System.out.print ("Enter the amout of loan borrowed: $");
double L = input.nextDouble();
//Prompt the user to enter the number of the months of the loan
System.out.print ("Enter the number of the months of the loan: ");
double n = input.nextDouble();
//Prompt the user to enter the annual interest rate
System.out.print ("Enter the annual interest rate: ");
double i = input.nextDouble();
System.out.println ("The monthly loan payment is " + monthlyPaymentCalculation (L,n,i));
}
public static double monthlyPaymentCalculation (double L, double n, double i)
{
double r = i / 12;
double P = ( r * L ) / ( 1 - Math.pow ((1+r),(-n)));
DecimalFormat df = new DecimalFormat ("#####.##");
return df.format (P); // returns value of P with two decimal place
}
}
应返回带有两个十进制值的P值,但它会不断出现required: double found: java.lang.String
错误
答案 0 :(得分:0)
format
返回String而不是double。将monthlyPaymentCalculation
的返回类型更改为String