Java中的变量声明和初始化

时间:2013-10-20 15:37:22

标签: java variables scope

我应该在我的代码中放置此命令?

Item item = new Item();

我应该把它放在我的循环do-while中,以便它继续创建一个新的Item类取决于用户他们想要输入另一个产品的次数。

问题是每当我将该命令放入do-while内时,它根据程序底部的代码找不到符号item。所以我应该把它放在哪里?

我的代码:

public static void main(String[] args) {

    Scanner scan = new Scanner(System. in );
    String prompt, id;
    int z = 0, choice, x = 999;
    int iprice;
    String icode;
    String iname;

    class Item {
        public int price;
        public String code;
        public String name;
    }

    ArrayList<Item> cart = new ArrayList<Item>(0);

    do {
        System.out.print("\nItem Code: ");
        icode = scan.next();
        System.out.print("Item Name: ");
        iname = scan.next();
        System.out.print("Item Price: ");
        iprice = scan.nextInt();

        item.code = icode;
        item.name = iname;
        item.price = iprice;

        cart.add(item);

        System.out.print("\tInput another? Y/N? ");
        prompt = scan.next();

    } while (prompt.equals("y"));

    System.out.println("\nWhat do you want to do?");
    System.out.println("\n1. Purchase Item ");
    System.out.println("2. List of Products ");
    System.out.println("3. Exit Program ");

    System.out.print("\n\tChoice: ");
    choice = scan.nextInt();
    if (choice == 1) {
        System.out.println("\nItem Code: ");
        id = scan.next();
        if (item.code.equals(id)) {
            System.out.println("Item Name: " + item.name);
            System.out.println("Item Price: " + item.price);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

你应该声明循环外的变量:

Item item;

然后你应该在循环的最开始初始化

item = new Item();

这样变量在整个类中都可见,所以你不会得到你所提到的错误...

此外,这基本上意味着您在每个循环迭代中创建一个新对象,并且您将此新对象分配给您之前声明的变量item,因此您的用户可以创建尽可能多的不同项目希望...

请注意,虽然您在每次迭代中覆盖变量item,但您不会丢失先前附加到变量{{1}的对象因为您在执行item时向ArrayList<Item> cart添加对该对象的引用...

您可能希望阅读this article关于Java,范围等中的类和对象的声明...

答案 1 :(得分:1)

这取决于。您可以将它放在循环外或类定义中,这样您就可以一次又一次地重复使用变量,并避免为每次传递创建局部变量。
类似于:

Item myItem;

do{
    myItem = new Item();
    // add perfumes and shoes
    // maybe GTA 5
}while(condition)  

你需要学习所谓的variable scope。也就是说,您可以在哪里访问变量。这里:http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

答案 2 :(得分:1)

在do-while循环Item item = new Item();内创建新的对象我在下面进行了更改...

public static void main(String[] args) {
            // TODO, add your application code
            Scanner scan = new Scanner (System.in);
            String prompt, id; 
            int z=0, choice, x=999;
            int iprice;
            String icode;
            String iname;


            class Item {
                public int price;
                public String code;
                public String name;

            }   

            ArrayList<Item> cart = new ArrayList<Item>(0);
             Item item; // type this
                do {

            System.out.print("\nItem Code: ");
            icode=scan.next();
            System.out.print("Item Name: ");
            iname=scan.next();
            System.out.print("Item Price: ");
            iprice=scan.nextInt();


            item = new Item();  //also create new Item(); here
            item.code=icode;
            item.name=iname;
            item.price=iprice;

            cart.add(item);

            System.out.print("\tInput another? Y/N? ");
            prompt=scan.next();
            }while (prompt.equals("y"));

            System.out.println("\nWhat do you want to do?");
            System.out.println("\n1. Purchase Item ");
            System.out.println("2. List of Products ");
            System.out.println("3. Exit Program ");
            System.out.print("\n\tChoice: ");
            choice=scan.nextInt();
             if (choice==1) {

            System.out.println("\nItem Code: ");
            id=scan.next();
                    if (item.code.equals(id)) {         
            System.out.println("Item Name: "+item.name);
            System.out.println("Item Price: "+item.price);

                    }       
             }






        }
        }