简体   繁体   English

将我的用户输入传递给方法时,如何获取用户输入以访问我的ArrayList

[英]How can I get my userinput to access my ArrayList when I pass it to a method

Here's the code I have so far. 这是我到目前为止的代码。 This is the error I get for line 58 这是我在第58行得到的错误

incompatible types required: ArrayList found: 要求的类型不兼容:找到ArrayList:
VendProduct 供应产品

(Alt-Enter shows hints) (Alt-Enter显示提示)

//A vending machine to dispense multiple products //可分配多种产品的自动售货机

package newvendingmachine;

import java.util.ArrayList;
import java.util.Scanner;

//Tony Moore 

public class NewVendingMachine{
    //Set class data
    static String[] product = {"Chips", "M&Ms", "Peanuts", "Popcorn", "Snickers"};
    static Float[] cost = {.50f, .75f, .75f, .75f, .90f};
    static Integer[] inventory = {20, 20, 20, 20, 20};

    /** 
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        ArrayList<VendProduct> vp = new ArrayList<>();
        for (int idx=0; idx<product.length; idx++) {
            vp.add(new VendProduct());
        }

        //Accept user input
        Scanner input = new Scanner(System.in);   
        int userInput;

        while (true) {
            // Display menu graphics

            System.out.println(" Options: Press your selection then press enter ");
            System.out.println(" 1. "+product[0]+" ");
            System.out.println(" 2. "+product[1]+" ");
            System.out.println(" 3. "+product[2]+" ");
            System.out.println(" 4. "+product[3]+" ");
            System.out.println(" 5. "+product[4]+" ");
            System.out.println(" 6. Quit ");


            userInput = input.nextInt(); 

            if (userInput <1 || userInput >6) {
                System.out.println("Please make a vaild selection");    
            }

            if (userInput >=1 || userInput <=6) {
                vp = (VendProduct)vp.get(userInput-1);
                vp.buyProduct();         
            }

            if (userInput == 6) {
                System.out.println("Thanks, and come back soon");
                break;
            }
        }   
    }
}

vp = (VendProduct)vp.get(userInput-1);

vp is an ArrayList of VendProduct, but you're trying to set it to just one VendProduct... vp是VendProduct的ArrayList,但是您试图将其设置为仅一个VendProduct。

You should create a variable of the type VendProduct and use that, like this: 您应该创建VendProduct类型的变量,并使用它,如下所示:

 VendProduct product = vp.get(userInput-1);
 product.buyProduct();

You could also do vp.get(userInput-1).buyProduct(); 您也可以执行vp.get(userInput-1).buyProduct(); directly in one line. 直接在一行中。 That's up to you. 随你(由你决定。 Using two lines usually makes the code a bit cleaner and easier to read. 通常使用两行代码会使代码更清晰,更易于阅读。

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

相关问题 当我希望线程共享ArrayList时,应在哪里实例化ArrayList,以及如何从main方法中访问它? - When I want my threads to share an ArrayList, where do I instantiate the ArrayList, and how do I access it from the main method? 当我的方法不在活动中时,如何将其传递给活动? - How can I pass my method an activity when it is not in an activity? Android如何在我的baseadapter中获取ArrayList Hashmap的值 - Android how can i get value of ArrayList Hashmap in my baseadapter 我怎样才能打印程序? 尝试访问.toString()时收到NullPointerException - How can I get my program to print? I get a NullPointerException when I try to access .toString() 我该如何更改我的数组列表? - How can i alter my arraylist? 我如何在arraylist上到达对象 - How can I reach my object at arraylist 我可以在方法 java class 中接受用户输入吗? - Can i take userinput in a method java class? 我如何获取值(ArrayList <Book> 书籍变量)在我的getBooks()中生成到我的onCreate方法中? - How do I get the values(ArrayList<Book> Books variable) generated in my getBooks() into my onCreate method? 我想将不同的对象传递给我的方法,如何解决ArrayList是原始类型…警告 - I want to pass different objects to my method, how to solve ArrayList is a raw type… warning 我怎样才能按字母顺序排列我的arraylist - How can i sort my arraylist alphabetical
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM