java - 不承认进口报关

标签 java

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

我删除了一些代码,这样它就不再那么困惑了;这不是我的整个类(class)。

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

最佳答案

这在语法上是不正确的:

NumberFormat fmt = new NumberFormat.getCurrencyInstance();

您没有新建 NumberFormat 的实例。 NumberFormat.getCurrencyInstance() 是一个方法调用,因此无法更新。

由于该方法已returns a static instance of NumberFormat ,继续并从声明中删除 new 关键字:

NumberFormat fmt = NumberFormat.getCurrencyInstance();

关于java - 不承认进口报关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18960825/

相关文章:

java - 尝试在我的程序中显示一棵树的成本

java - 当类字段的数据类型更改时处理反序列化

java - 如何为 Dropwizard 应用程序构建 web.xml

java - jsp页面如何调用类中的方法

java - 从许多队列中消费

java - MVC结构中图形的问题

java - 如何使用 Java 线程运行 javafx 组件?

java - 高效监控 int 值

使用多个选择框的 JavaFX 过滤 ListView

java - [警告][插件]插件 jdbc- River,无法调用自定义 onModule 方法