学不会编程的小男孩 2017-02-06 07:21 采纳率: 90.9%
浏览 1874
已采纳

最后两语句的(int),加了结果有两位小数,不加有很多小数;但是int不是整数型的吗

package programList;
import java.util.Scanner;
public class money {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

    System.out.print("Enter annual interest rate, eg:7.25% : ");    //输入年利率
    double annualInterestRate = input.nextDouble();

    double monthlyInterestRate = annualInterestRate / 1200 ;

    System.out.print("Enter number of years as an integer, eg:5 : ");   //输入整年数
    int numberOfYears = input.nextInt();

    System.out.print("Enter loan amount,eg:120000.95 : ");  //输入贷款金额
    double loanAmount = input.nextDouble();

    double monthlyPayment = loanAmount * monthlyInterestRate / (1-1 / Math.pow(1+monthlyInterestRate, numberOfYears * 12));
    double totalPayment = monthlyPayment * numberOfYears * 12 ;

    //每月支付金额
    System.out.println("The monthly payment is $" + (int)(monthlyPayment * 100) / 100.0);
    //总付款金额
    System.out.println("The total payment is $" + (int)(totalPayment * 100) / 100.0);
}

}

  • 写回答

5条回答 默认 最新

  • 当作看不见 2017-02-06 07:21
    关注

    int型的无论乘除加减float型的(double,long)都会隐式转换为对应的类型
    众所周知, 1 是整形,但是1.00是float类型的,所以整形除以一个float,就会发生隐式转化成float类型

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?