0

当我知道我正确导入了 java.text.NumberFormat 时,控制台告诉我找不到符号“getCurrencyInstance()”

我删除了一些代码,所以它没有那么混乱;这不是我的全班。

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

public class Kohls
{

    // initialization
    static Prompter prompter;
    static Calculator calc;
    static Operator operator;

    private enum cardColor
    {
        RED, BLUE, GREEN;
    } // end of enum Color

    private static class Calculator
    {
        public int getDiscount(int age, cardColor color)
        {
            if (age > 62)
            // senior discount
                return 20;

            if (color == cardColor.RED)
            {
                return 30;
            }
            else if (color == cardColor.BLUE)
            {
                return 25;
            }
            else if (color == cardColor.GREEN)
            {
                return 15;
            }
            return 0;
        }

        public double getSalePrice(int discountPercentage, double price)
        {
            double salePrice = price - (price * (discountPercentage / 100));
            return salePrice;
        }
    } // end of class Calculator

    private class Operator
    {
        public void getPriceWithDiscount()
        {
            // prompts
            double price = prompter.getPrice();
            int age = prompter.getAge();
            cardColor color = prompter.getColor();

            // discount(s)
            int discountPercentage = calc.getDiscount(age, color);
            double salePrice = calc.getSalePrice(discountPercentage, price);

            NumberFormat fmt = new NumberFormat.getCurrencyInstance();
            String salePriceFormat = fmt.format(salePrice);

            operator.display(discountPercentage, salePriceFormat);
        }

        public void display(int discountPercentage, String salePrice)
        {
            System.out.print("You saved " + discountPercentage + "% on your purchase.");
            System.out.print("\nThe price of your purchase with discount is " + salePrice + ".");
        }
    } // end of class Operator

    public Kohls()
    {
        prompter = new Prompter();
        calc = new Calculator();
        operator = new Operator();
    } // end of constructor

    public static void main(String[] args)
    {
        Kohls kohls = new Kohls();
        kohls.operator.getPriceWithDiscount();
    } // end of method main()
} // end of class Kohls
4

5 回答 5

4

这在语法上是不正确的:

NumberFormat fmt = new NumberFormat.getCurrencyInstance();

您没有更新NumberFormat. NumberFormat.getCurrencyInstance()是一个方法调用,因此不能被更新。

由于该方法已经返回 的静态实例NumberFormat,请继续从声明中删除new关键字:

NumberFormat fmt = NumberFormat.getCurrencyInstance();
于 2013-09-23T13:46:16.750 回答
3

删除行中的新运算符。它是一个静态方法,应该以静态方式访问。此外,NumberFormat 是一个抽象类,您也不能实例化它。

NumberFormat nf = NumberFormat.getCurrencyInstance();
于 2013-09-23T13:48:47.573 回答
1

不要做

new NumberFormat.getCurrencyInstance();

为一种static方法。做

NumberFormat.getCurrencyInstance();
于 2013-09-23T13:46:04.980 回答
0

您不应该按new原样getCurrencyInstance()使用static

改变

NumberFormat fmt = new NumberFormat.getCurrencyInstance();

NumberFormat fmt = NumberFormat.getCurrencyInstance();

于 2013-09-23T14:07:30.263 回答
0

这条线

NumberFormat fmt = new NumberFormat.getCurrencyInstance();

应该

NumberFormat fmt = NumberFormat.getCurrencyInstance();

因为getCurrencyInstance()被声明为静态的。

希望这可以帮助。

于 2013-09-23T13:47:39.113 回答