使用NumberFormat时,Locale无法解析为变量么?

时间:2017-01-02 19:11:57

标签: java

我使用以下代码获取加拿大抵押贷款付款,我收到消息"无法解决变量"在第15行。我做错了什么? 我的电脑设置为加拿大法语。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.Currency;

public class Mortgage {
    public static void main(String args[]) throws IOException {
        NumberFormat canadaFrench = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
        double principal, rate, ratePercent, rateFinal;
        int years, n;
        final int paymentsPerYear = 12;
        final int timesPerYearCalculated = 12;
        double effectiveAnnualRate;
        double payment;

        System.out.print("How much are you borrowing?            ");
        principal = Double.parseDouble(keyboard.readLine());
        System.out.print("What's the interest rate?              ");
        ratePercent = Double.parseDouble(keyboard.readLine());
        rate = ratePercent / 100.00;
        System.out.print("How many years are you taking to pay?  ");
        years = Integer.parseInt(keyboard.readLine());
        System.out.println("------------------------------");

        n = paymentsPerYear * years;
        // Hypothèque canadienne avec un taux capitalisé deux fois l'an,
        // d'où le calcul de l'équivalance des taux!
        rateFinal = 12.0 * (Math.pow(1.0 + (rate / 2.0), (2.0 / 12.0)) - 1.0);
        effectiveAnnualRate = rateFinal / paymentsPerYear;

        payment = principal * (effectiveAnnualRate / (1 - Math.pow(1 + effectiveAnnualRate, -n)));

        System.out.print("Your monthly payment is                ");
        System.out.println(canadaFrench.format(payment));
        System.out.println();
    }
}

1 个答案:

答案 0 :(得分:1)

您需要添加import java.util.Locale;