0

这是我第一次写课程,我遇到了所有这些错误:

C:\Users\Eamon\programming\java>javac -Xlint:unchecked Shop1.java
Shop1.java:20: error: cannot find symbol
                if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
                                                            ^
  symbol:   variable quantity
  location: class Object
Shop1.java:21: error: cannot find symbol
                    System.out.println(cart.itemsInCart.products.get(itemIndex).quantity
                                                                               ^
  symbol:   variable quantity
  location: class Object
Shop1.java:22: error: cannot find symbol
                        + " " + cart.itemsInCart.products.get(itemIndex).name
                                                                        ^
  symbol:   variable name
  location: class Object
Shop1.java:23: error: cannot find symbol
                        + " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price)
                                                                                  ^
  symbol:   variable price
  location: class Object
Shop1.java:25: error: cannot find symbol
                        ((cart.itemsInCart.products.get(itemIndex).quantity
                                                                  ^
  symbol:   variable quantity
  location: class Object
Shop1.java:26: error: cannot find symbol
                        * cart.itemsInCart.products.get(itemIndex).price)));
                                                                  ^
  symbol:   variable price
  location: class Object
Shop1.java:35: error: cannot find symbol
                subtotal += cart.itemsInCart.products.get(itemIndex).quantity
                                                                    ^
  symbol:   variable quantity
  location: class Object
Shop1.java:36: error: cannot find symbol
                * cart.itemsInCart.products.get(itemIndex).price;
                                                          ^
  symbol:   variable price
  location: class Object
Shop1.java:126: error: cannot find symbol
                    if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
                                                                                 ^
  symbol:   variable code
  location: class Object
Shop1.java:138: error: cannot find symbol
            this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
                                                    ^
  symbol:   variable quantity
  location: class Object
Shop1.java:140: error: cannot find symbol
            if (this.itemsInCart.products.get(itemIndex).quantity > 100)
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:143: error: cannot find symbol
                    + this.itemsInCart.products.get(itemIndex).quantity
                                                              ^
  symbol:   variable quantity
  location: class Object
Shop1.java:145: error: cannot find symbol
                    + this.itemsInCart.products.get(itemIndex).quantity
                                                              ^
  symbol:   variable quantity
  location: class Object
Shop1.java:147: error: cannot find symbol
                if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
                                                                                 ^
  symbol:   variable quantity
  location: class Object
Shop1.java:148: error: cannot find symbol
                    this.item[itemIndex].quantity = 0;
                        ^
  symbol: variable item
Shop1.java:150: error: cannot find symbol
            if (this.itemsInCart.products.get(itemIndex).quantity < 0)
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:151: error: cannot find symbol
                this.itemsInCart.products.get(itemIndex).quantity = 0;
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:50: error: cannot find symbol
        ArrayList products = new Arraylist<Product>(3);
                                 ^
  symbol:   class Arraylist
  location: class Catalogue
Shop1.java:54: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Condensed Powdered water", "P3487", 2.50
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:56: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Distilled Moonbeams", "K3876", 3.00
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:58: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:80: error: Illegal static declaration in inner class Catalogue.Product
            static final Pattern productCodeRegex =
                                 ^
  modifier 'static' is only allowed in constant variable declarations
Shop1.java:83: error: Illegal static declaration in inner class Catalogue.Product
            public static boolean isValidCode(String codeInput)
                                  ^
  modifier 'static' is only allowed in constant variable declarations
Shop1.java:92: error: cannot find symbol
                    + this.products.get(itemIndex).name
                                                  ^
  symbol:   variable name
  location: class Object
Shop1.java:93: error: cannot find symbol
                    + " [" + this.products.get(itemIndex).code + "], $"
                                                         ^
  symbol:   variable code
  location: class Object
Shop1.java:94: error: cannot find symbol
                    + df.format(this.products.get(itemIndex).price) + " "
                                                            ^
  symbol:   variable price
  location: class Object
Shop1.java:95: error: cannot find symbol
                    + this.products.get(itemIndex).rate + ".");
                                                  ^
  symbol:   variable rate
  location: class Object
24 errors
3 warnings

我正在回去工作,我可以很好地找到这些变量;是什么赋予了?

import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.text.DecimalFormat;

public class Shop1
{  
    public static void main(String args[])
    {  
        Cart cart = new Cart(new Catalogue());
        printOrder(cart);
    }

    public static void printOrder(Cart cart)
    {
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("Your order:");
        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
                System.out.println(cart.itemsInCart.products.get(itemIndex).quantity 
                    + " " + cart.itemsInCart.products.get(itemIndex).name 
                    + " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price) 
                    + " = $" + df.format
                    ((cart.itemsInCart.products.get(itemIndex).quantity 
                    * cart.itemsInCart.products.get(itemIndex).price)));

        double subtotal = 0;
        int taxPercent = 20;
        double tax;
        double total;

        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            subtotal += cart.itemsInCart.products.get(itemIndex).quantity 
            * cart.itemsInCart.products.get(itemIndex).price;
        tax = subtotal * taxPercent / 100;
        total = subtotal + tax;


        System.out.print("Subtotal: $" + df.format(subtotal) 
            + " Tax @ " + taxPercent + "%: $" + df.format(tax) 
            + " Grand Total: $" + df.format(total));
    }  
}

class Catalogue
{
    DecimalFormat df = new DecimalFormat("0.00");
    ArrayList products = new Arraylist<Product>(3);

    public Catalogue()
    {
        products.add(new Product("Condensed Powdered water", "P3487", 2.50
            , "per packet"));
        products.add(new Product("Distilled Moonbeams", "K3876", 3.00
            , "a dozen")); 
        products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
            , "for 60"));
    }

    class Product
    {
        String name;
        double price;
        String code;
        String rate;
        int quantity;

        public Product(String startName, String startCode, double startPrice
            , String startRate) 
        {
            name = startName;
            code = startCode;
            price = startPrice;
            rate = startRate;
            quantity = 0;
        }

        static final Pattern productCodeRegex = 
            Pattern.compile("^[a-zA-Z][0-9]{4}$"); 

        public static boolean isValidCode(String codeInput)
            {return productCodeRegex.matcher(codeInput).matches();}
    }

    public void printCatalogue()
    {
        System.out.println("Our catalogue (product codes in brackets):");
        for(int itemIndex = 0; itemIndex < this.products.size(); itemIndex++)
            System.out.println("(" + (itemIndex + 1) + ") " 
                + this.products.get(itemIndex).name 
                + " [" + this.products.get(itemIndex).code + "], $"
                + df.format(this.products.get(itemIndex).price) + " " 
                + this.products.get(itemIndex).rate + ".");
        System.out.println("Buy something!");
    }
}

class Cart
{
    Scanner scanner = new Scanner(System.in); //

    int size = 3; //
    String codeInput = "";
    Catalogue itemsInCart;

    public Cart(Catalogue catalogue) 
    {
        itemsInCart = catalogue;
        catalogue.printCatalogue();
        this.selectProducts();
    }

    public void selectProducts()
    {
        while (true)
        {
            System.out.print("Enter product code (0 to check out): ");
            codeInput = scanner.next();
            scanner.nextLine();
            if (codeInput.equals("0")) return;

            for (int itemIndex = 0; itemIndex < this.itemsInCart.products.size(); 
                itemIndex++)
                if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
                    this.addToCart(itemIndex);       
            if (Product.isValidCode(codeInput))
                System.out.println("This product code is not on record.");      
            else System.out.println
                ("Sorry, I don't understand! Use product codes only.");
        }         
    }

    public void addToCart(int itemIndex)
    {
        System.out.print("Enter quantity: ");
        this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
        scanner.nextLine();
        if (this.itemsInCart.products.get(itemIndex).quantity > 100)
        {
            System.out.print("That is a large order, " 
                + this.itemsInCart.products.get(itemIndex).quantity 
                + " counts. Is this correct? Enter \"" 
                + this.itemsInCart.products.get(itemIndex).quantity 
                + "\" to confirm, or, enter any other integer to cancel: ");
            if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
                this.item[itemIndex].quantity = 0;
        }
        if (this.itemsInCart.products.get(itemIndex).quantity < 0)
            this.itemsInCart.products.get(itemIndex).quantity = 0;
    }

}
4

2 回答 2

4

Catalogue,你需要

ArrayList<Product> products = new ArrayList<Product>(3);

代替

ArrayList products = new Arraylist<Product>(3);

否则编译器不知道将返回哪种类型

cart.itemsInCart.products.get(itemIndex)

只有编译器知道这里返回的类型是Product,他才知道您可以访问该quantity字段。如果可以返回任何类型,则可能是类型.quantity不存在或不可访问。

为了将来参考,请提供您实际使用的代码,因为您的代码甚至无法编译。你有Arraylist它应该在的地方ArrayList(Java 区分大小写)。对接口编程也是一个好习惯,所以最好写List<Product> products = new ArrayList<Product>(3);

于 2013-01-03T10:50:38.297 回答
1

据我所知,您尝试使用的某些符号可能无法访问。尝试在自己的文件中定义每个类,然后制作要使用的符号public,看看是否能解决问题。之后,您可以以更好的方式构建代码(即使用 getter 和 setter,而不是直接访问)。

编辑:我的错误。jlordo的答案是正确的。

于 2013-01-03T10:49:13.910 回答