当我知道我正确导入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
答案 0 :(得分:4)
这在语法上是不正确的:
NumberFormat fmt = new NumberFormat.getCurrencyInstance();
您没有新手NumberFormat
的实例。 NumberFormat.getCurrencyInstance()
是方法调用,因此无法新建。
由于该方法已经returns a static instance of NumberFormat
,请继续并从声明中删除new
关键字:
NumberFormat fmt = NumberFormat.getCurrencyInstance();
答案 1 :(得分:3)
删除该行中的新运算符。它是一种静态方法,应该在静态的情况下访问。更重要的是,NumberFormat是一个抽象类,你也无法实例化它。
NumberFormat nf = NumberFormat.getCurrencyInstance();
答案 2 :(得分:1)
不要做
new NumberFormat.getCurrencyInstance();
表示static
方法。做
NumberFormat.getCurrencyInstance();
答案 3 :(得分:0)
这一行
NumberFormat fmt = new NumberFormat.getCurrencyInstance();
应该是
NumberFormat fmt = NumberFormat.getCurrencyInstance();
因为getCurrencyInstance()
被声明为静态。
希望这有帮助。
答案 4 :(得分:0)
您不应该使用new
,因为getCurrencyInstance()
是static
更改
NumberFormat fmt = new NumberFormat.getCurrencyInstance();
到
NumberFormat fmt = NumberFormat.getCurrencyInstance();