Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加arrayList.set的用户输入_Java_Arrays - Fatal编程技术网

Java 添加arrayList.set的用户输入

Java 添加arrayList.set的用户输入,java,arrays,Java,Arrays,我一直在做以下任务: 这个程序应该创建一个名为Book List的ArrayList。节目 应显示一个菜单,允许用户从以下选项中进行选择: 输入1将书本添加到列表中: 输入2可将书本编辑到列表中: 输入3从列表中删除一本书: 输入4以显示书籍列表: 输入5退出: 程序应使用case/switch语句并打开用户选择。程序应继续,直到用户输入5退出 案例1应使用BookList.add将书籍添加到ArrayList 案例2应使用BookList.set将书籍编辑到ArrayList 案例3应使用Bo

我一直在做以下任务: 这个程序应该创建一个名为Book List的ArrayList。节目 应显示一个菜单,允许用户从以下选项中进行选择:

输入1将书本添加到列表中:

输入2可将书本编辑到列表中:

输入3从列表中删除一本书:

输入4以显示书籍列表:

输入5退出:

程序应使用case/switch语句并打开用户选择。程序应继续,直到用户输入5退出

  • 案例1应使用BookList.add将书籍添加到ArrayList
  • 案例2应使用BookList.set将书籍编辑到ArrayList
  • 案例3应使用BookList.remove从列表中删除名称(要求用户输入要从ArrayList中删除的书籍的索引号)
  • 案例4应该使用for循环来显示ArrayList中的所有书籍 以及他们的索引号
  • (示例输出)您的输出应类似于:

    **********书单*********

    索引:0名称:异乡的陌生人

    索引:1名称:PHP和MySQL

    索引:2名称:HTML和CSS

    索引:3名称:爱情故事

    索引:4名称:地球静止的那一天

    我在ArrayList.set上遇到问题。我不知道如何使用用户输入(索引号和更正的书名)来更新数组列表。这并不是我关于这个程序的问题的结束,但是如果能在ArrayList.set上得到任何帮助,我将不胜感激

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class BookList {
    
        public static void main(String[] args) {
    
            Scanner keyboard = new Scanner(System.in);
            Scanner input = new Scanner(System.in);
    
            //Create array
            ArrayList<String> bookList = new ArrayList<String>();
    
            //Add a few books to the array bookList
            bookList.add("A Game of Thrones");
            bookList.add("A Clash of Kings");
            bookList.add("A Storm of Swords");
            bookList.add("A Feast for Crows");
            bookList.add("A Dance with Dragons");
    
            //Display the items in bookList array and their indices.
            System.out.println("******** The Book List ********");
            for (int index = 0; index < bookList.size(); index++)
            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
            System.out.print("\n");
    
    
            //declaring variables
            int menuID = 0;
            int changeIndex = 0;
            int delIndex = 0;
            String addTitle = "";
            String corrTitle = "";
    
    
            //Menu
            System.out.println("Enter 1 to add a book to the list");
            System.out.println("Enter 2 to edit a book in the list");
            System.out.println("Enter 3 to remove a book from the list");
            System.out.println("Enter 4 display the list of books");
            System.out.println("Enter 5 to quit");
            System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
            menuID = input.nextInt();
    
            while (menuID != 0)
            {
    
                if (menuID >=1 && menuID <= 4)
                {
                    switch (menuID)
                    {
                        case 1:
                            System.out.println("Enter the book to add: ");
                            addTitle = keyboard.nextLine();
                            bookList.add(addTitle);
    
                            System.out.print("\n");
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 2:
                            System.out.println("Enter index number of the book to change: ");
                            changeIndex = keyboard.nextInt();
    
                            System.out.println("Enter the corrected book name: ");
                            corrTitle = keyboard.nextLine();
    
                            bookList.set(changeIndex, corrTitle);
    
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 3:
                            System.out.println("Enter index number of the book to remove: ");
                            delIndex = keyboard.nextInt();
                            bookList.remove(delIndex);
    
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 4:
                            System.out.println("******** The Book List ********");
                            for (int index = 0; index < bookList.size(); index++)
                            {System.out.println("Index:  " + index + "    Name: " + bookList.get(index));}
                            break;
    
                        case 5:
                            System.out.println("Goodbye!");
                            break;
                    }}
                else if (menuID >=1 && menuID <= 4)
                    System.out.println("You must enter a number 1-5:");
                System.out.println("Enter a menu number (1-4 or 5 to Exit): ");
                menuID = input.nextInt();
    
            }
        }
    }
    
    import java.util.ArrayList;
    导入java.util.Scanner;
    公共课书目{
    公共静态void main(字符串[]args){
    扫描仪键盘=新扫描仪(System.in);
    扫描仪输入=新扫描仪(System.in);
    //创建数组
    ArrayList bookList=新建ArrayList();
    //向数组图书列表中添加一些图书
    书单。添加(“权力游戏”);
    书单。添加(“国王的冲突”);
    书单。加上(“刀剑风暴”);
    书单。添加(“乌鸦的盛宴”);
    增加(“与龙共舞”);
    //显示图书列表数组中的项目及其索引。
    System.out.println(“*********图书列表*******”);
    对于(int index=0;index如果(menuID>=1&&menuID=1&&menuID您应该只使用一个具有相同源的
    扫描器
    对象,除非您有一种非常复杂的方法来遍历输入,这里可能不是这样。因此您的代码应该如下所示:

    // Scanner keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    // ...
    menuID = input.nextInt();
    
    while (menuID != 0)
    {
        if (menuID >=1 && menuID <= 4)
        {
            switch (menuID)
            {
                case 1:
                    // ...
                    addTitle = input.nextLine();
    // ...
    
    menuID = input.nextInt();
    input.nextLine(); // Consumes the rest of the line
    
    menuID = Integer.parseInt(input.nextLine());
    
    或者,您可以每次获取整行并使用
    integer.parseInt
    解析其中包含的整数,如下所示:

    // Scanner keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    // ...
    menuID = input.nextInt();
    
    while (menuID != 0)
    {
        if (menuID >=1 && menuID <= 4)
        {
            switch (menuID)
            {
                case 1:
                    // ...
                    addTitle = input.nextLine();
    // ...
    
    menuID = input.nextInt();
    input.nextLine(); // Consumes the rest of the line
    
    menuID = Integer.parseInt(input.nextLine());
    
    使用第二个选项(我发现在这种情况下更好)


    作为旁注,您要求用户使用
    5
    退出,但只要
    menuID
    不是
    0

    ,您的
    while
    将继续运行。请阅读:。不要将整个代码都转储到我们身上。您了解了方法吗?如果了解,您应该创建一个方法,例如名为
    listBooks()
    ,因此不必重复打印图书列表的逻辑4次。