2013-09-23 69 views
0

控制檯告訴我它無法找到符號「getCurrencyInstance()」當我知道我正確導入java.text.NumberFormat中不承認進口報關

我刪除了一些代碼,所以它並不像雜亂;這不是我全班的。

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 

回答

1

不要爲static方法做

new NumberFormat.getCurrencyInstance(); 

。做

NumberFormat.getCurrencyInstance(); 
4

這是語法不正確:

NumberFormat fmt = new NumberFormat.getCurrencyInstance(); 

你是不是newing的NumberFormat一個實例。 NumberFormat.getCurrencyInstance()是一個方法調用,因此不能被新增。

由於該方法已經returns a static instance of NumberFormat,繼續前進,從聲明刪除new關鍵字:

NumberFormat fmt = NumberFormat.getCurrencyInstance(); 
0

此行

NumberFormat fmt = new NumberFormat.getCurrencyInstance(); 

應該

NumberFormat fmt = NumberFormat.getCurrencyInstance(); 

因爲getCurrencyInstance()聲明靜態的。

希望這會有所幫助。

3

在行中刪除新的運營商。這是一種靜態方法,應該以靜態方式訪問。更重要的是,NumberFormat是一個抽象類,你不能實例化它。

NumberFormat nf = NumberFormat.getCurrencyInstance(); 
0

你不應該使用new作爲getCurrencyInstance()static

變化

NumberFormat fmt = new NumberFormat.getCurrencyInstance();

NumberFormat fmt = NumberFormat.getCurrencyInstance();