我不确定课程是如何运作的。为什么我的java文件不能编译?

时间:2015-12-18 22:40:45

标签: java class

import java.util.Scanner;

public class Item {
public static void main(String[] arrstring) {
    Item[] item2 = new Item[]{new Item("eggs", 2.99), new Item("milk", 3.99),
     new Item("broccoli", 2.75), new Item("bread", 3.25), new Item("apple", 1.25)};
    Cart cart = new Cart();
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the virtual grocery store!");
    int x = 0;
    while (x != 4) {
        int y;
        System.out.println("\nWhat would you like to do?");
        System.out.println("1: View available items");
        System.out.println("2: View your cart");
        System.out.println("3: Add an item to your cart");
        System.out.println("4: Check out");
        System.out.print("Enter your choice: ");
        x = input.nextInt();
        if (x == 1) {
            System.out.println("\nAvailable Items:");
            for (y = 0; y < item2.length; y++) {
                System.out.print("" + y + ": ");
                items[y]Item.print();
            }
            continue;
        }
        if (x == 2) {
            Cart.printItems();
            System.out.println("Total cost: $" + Cart.getTotal());
            continue;
        }
        if (x == 3) {
            System.out.print("\nWhich item would you like to add? ");
            y = input.nextInt();
            Cart.addItem(item2[y]);
            continue;
        }
        if (x != 4) continue;
        System.out.println("\nThat will be $" + cart.getTotal() + ".");
        System.out.println("Thank you!");
    }
}
}
class Cart {
private Item[] items = new Item[100];
private int numItems = 0;

public void addItem(Item item) {
    items[numItems] = item;
    numItems++;
}
public void printItems() {
    if (numItems == 0) {
        System.out.println("\nYour cart is empty.");
    } else {
        System.out.println("\nYour cart:");
        for (int i = 0; i < numItems; i++) {
            items[i]Item.print();
        }
    }
}

public double getTotal() {
    double d = 0.0;
    for (int i = 0; i < numItems; i++) {
        d += items[i]Item.getPrice();
    }
    return d;
}
}
class Item {
private String name;
private double price;

public Item(String string, double j) {
    name = string;
    price = j;
}

public void print() {
    System.out.println(name + ": $" + price);
}

public double getPrice() {
    return price;
}
}

错误:

Item.java:24: error: not a statement
                items[y]Item.print();
                     ^
Item.java:24: error: ';' expected
                items[y]Item.print();
                        ^
Item.java:59: error: not a statement
            items[i]Item.print();
                 ^
Item.java:59: error: ';' expected
            items[i]Item.print();
                    ^
4 errors

我不确定我的课程出错了,并将其调回主要方法。任何帮助深表感谢。我正在尝试使用上面数组中包含的项目制作虚拟购物车。我还希望能够看到购物车中的每件商品并计算总数。代码的底部有我的错误消息。

1 个答案:

答案 0 :(得分:0)

Item有两个定义。必须更改一个名称:

这在开头看来似乎是第二个Item构造函数:

new Item("eggs", 2.99)

这是无效的:items [i] Item.getPrice();

购物车未定义。