自存 mosh java course homework 1

import java.text.NumberFormat;
import java.util.Scanner;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        /*已经对了 完成了 100%和结果一样,前面出错原因是公式看错写错 算出来永远是0*/
//        Scanner scanner = new Scanner(System.in); //获得终端
//        System.out.print("Principal:(本金)");
//        int principal = scanner.nextInt();// 用户输入的本金
//
//        System.out.print("Annual Interest Rate:(年利率)"); //公式里的是月利率 所以需要除以12,用户输入的是百分数 需要再除以100
//        double AnnualInterestRate = scanner.nextDouble();// 用户输入的年利率
//        double r = AnnualInterestRate / 12 / 100;
//
//        System.out.print("Period:(还款年数)");
//        int Period = scanner.nextInt();// 用户输入的还款年数
//        int n = Period * 12;  //以每月为一次,的还款次数
//
//
//        double molecule = Math.pow((1 + r),n) ; //公式的分子 幂运算 Math.pow()
//        double denominator = Math.pow((1 + r), n) - 1;   //  公式的分母
//
//
//        double Mortgage =(principal*molecule*r)/denominator;
//        NumberFormat currency = NumberFormat.getCurrencyInstance();
//        String result = currency.format(Mortgage);
//        System.out.println("Mortgage:(每月付)"+result);


        /*mosh的版本*/
        final byte MONTH_IN_YEAR = 12;
        final byte PERCENT =100;
        Scanner scanner = new Scanner(System.in); //获得终端
        System.out.print("Principal:(本金)");
        int principal = scanner.nextInt();// SHORT 不够储存

        System.out.print("Annual Interest Rate:(年利率)");
        float AnnualInterestRate = scanner.nextFloat();// 因为利率是很小的数字 所以float就够了
        float r = AnnualInterestRate / PERCENT / MONTH_IN_YEAR;

        System.out.print("Period:(还款年数)");
        byte Period = scanner.nextByte();// 支持最大的数是30 byte就够了
        int n = Period * 12;   


        double molecule = Math.pow((1 + r), n); //公式的分子 幂运算 Math.pow()
        double denominator = Math.pow((1 + r), n) - 1;   //  公式的分母


        double Mortgage = (principal* r * molecule ) / denominator;
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        String result = currency.format(Mortgage);
        System.out.println("Mortgage:(每月付)" + result);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值