简体   繁体   English

如何以不同的方法使用 arraylist 但方法在 class 内部

[英]How to use arraylist in different methods but the methods are inside of the class

I want to use the Arraylist in different method to add into another Arraylist just like ordering but the error is always cannot find symbol for calling Arraylist in different methods.我想使用 Arraylist 以不同的方法添加到另一个 Arraylist 就像订购一样,但错误总是找不到在不同方法中调用 Arraylist 的符号。

import java.util.*;导入 java.util.*;

class SelectMenu{ class 选择菜单{

String prod;
double cost;

This is for Selecting Utilities:这是用于选择实用程序:

public void selectMenu(){

    int utility;    
    Scanner in = new Scanner(System.in);

    System.out.printf("\n\n[1]Ordering\n[2]Add Item\n[3]Remove Item\n[4]Show Orders\n[5]Print Receipt");
    do{System.out.printf("\n\nChoose Utility:");
    utility = in.nextInt();
    }while(utility < 1 || utility > 5);

    switch(utility){
        case 1:
            System.out.println("\t\t\t------------------------HOTDOGGU HOUSE--------------------------");
            displayMenu();
            getOrder();
            selectMenu();
        break;
        case 2:
            \\adding Item
        break;
        case 3:
            \\Remove Item
        break;
        case 4:
            displayOrders();
        break;
        case 5:
            \\Printing Receipt
        break;
    }

}

This is for Displaying Menu:这是用于显示菜单:

public void displayMenu(){
        ArrayList<String> menu1 = new ArrayList<String>();
        String[] menu_name = new String[6];
        menu1.add("");
        menu1.add("Hotdog");
        menu1.add("Cheesedog");
        menu1.add("Corndog");
        menu1.add("Sausage");
        menu1.add("Chickendog");

        ArrayList<Double> menu2 = new ArrayList<Double>();
        Double[] menu_price = new Double[6];

        menu2.add(0.00);
        menu2.add(25.50);
        menu2.add(30.45);
        menu2.add(20.15);
        menu2.add(50.25);
        menu2.add(35.75);

        System.out.println("\t\t\tOrder No."+"\t\tProduct"+"\t\t\t    Price");

    for (int i = 1; 1 < menu1.size() && i < menu2.size(); i++){
        System.out.printf("\n\t\t\t  ["+i+"]"+"\t\t  %s\t\t\t"+"  %2.2f PHP",menu1.get(i), menu2.get(i));
    }
}

This is for Displaying Orders:这是用于显示订单:

public void displayOrders(){

    ArrayList<String> product = new ArrayList<String>();
    for (int i = 1; i < product.size(); i++){
        product.add(prod);
    }

    ArrayList<Double> price = new ArrayList<Double>();
    for (int i = 1; i < price.size(); i++) {
        price.add(cost);
    }

    System.out.println("\t\t\tOrder No."+"\t\tProduct"+"\t\t\t    Price");

    for (int i = 1; i < product.size() && i < price.size(); i++){
        System.out.printf("\n\t\t\t  ["+i+"]"+"\t\t  %s\t\t\t"+"  %2.2f PHP",product.get(i), price.get(i));
     }
}

This is for getting orders:这是为了获得订单:

public void getOrder(){
    int order;

    Scanner g1 = new Scanner(System.in);

    do{System.out.printf("\nWhat is your Order?:");
    order = g1.nextInt();
    }while(order < 1 || order > 6);

    switch(order){
        case 1:
            prod = menu_name.get(order); \\Error cannot find symbol
            cost = menu_price.get(order); \\Error cannot find symbol
        break;
    }
}

} }

You have two options:你有两个选择:

  1. Make it a field variable, or使其成为字段变量,或
  2. Pass it in as a parameter to every method that uses the ArrayList.将其作为参数传递给使用 ArrayList 的每个方法。

Of the two, I would definitely recommend that you use option 1 because it is much easier.在这两者中,我绝对建议您使用选项 1,因为它更容易。 All that you have to do is this:您所要做的就是:

class SelectMenu {
    String prod;
    double cost; //these two field variables that you already have
    ArrayList<String> menu1; //declare it as a field variable.

/*
    All of your other methods here
*/
}

When you do this, you don't need to worry about anything else, but when you initialize it (ie give it a value) inside method selectMenu() , make sure you do this: menu1 = new ArrayList<String>();当你这样做时,你不需要担心其他任何事情,但是当你在方法selectMenu()中初始化它(即给它一个值)时,请确保你这样做: menu1 = new ArrayList<String>(); , and not this: ArrayList<String> menu1 = new ArrayList<String>(); ,而不是这个: ArrayList<String> menu1 = new ArrayList<String>(); . .

Because you already have two field variables in your program, I doubt that you would need help understand how these work.因为您的程序中已经有两个字段变量,所以我怀疑您是否需要帮助来理解它们是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM