【问题标题】:How would I display the elements of an arraylist of size i, into a case switch system?如何将大小为 i 的数组列表的元素显示到案例切换系统中?
【发布时间】:2015-12-10 17:48:29
【问题描述】:

我一直在研究将书籍对象存储到图书馆中的程序,但我陷入了停滞。

我需要从我的数组列表 BookList 中打印出所有大小未知的 Book 对象(由于可能包括将新书籍添加到系统中的功能)到案例切换系统中,以便您可以选择每个选项并进行编辑详情。

我是否需要找到 ArrayList.length 并使用它来确定 switch 语句选项的数量?

我的代码在下面

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

public class Library_Tester {


public static void main (String[] args){





    System.out.println(" ===========================================");
    System.out.println("|  ==  ==  ===== ==    ==       ====        |");
    System.out.println("|  ==  ==  ==    ==    ==     =      =      |");
    System.out.println("|  ======  ====  ==    ==    =        =     |"); 
    System.out.println("|  ==  ==  ==    ==    ==     =      =      |");
    System.out.println("|  ==  ==  ===== ===== =====    ====        |");
    System.out.println(" ===========================================");



        System.out.println(" =======================================");
        System.out.println("|        Library Systems Inc            |");
        System.out.println(" =======================================");
        System.out.println("| Options:                              |");
        System.out.println("|       1. Add a book                   |");
        System.out.println("|       2. Edit a book's details        |");
        System.out.println("|       3. Delete a book                |");
        System.out.println("|       4. Loan a book                  |");
        System.out.println("|       5. Return a book                |");
        System.out.println("|       6. Exit the Program             |");
        System.out.println("|       7. Display all books in library |");
        System.out.println("|                                       |");
        System.out.println("|  *Type a number to make a selection*  |");
        System.out.println(" =======================================");
        System.out.println("");
        System.out.print("Selection: ");

        Book a = new Book();

        a.setTitle("Random ");
        a.setAuthor("Craig Robertson");
        a.setBookID("1847398812");
        a.setonLoan(false);
        a.setNumberofLoans(3);

        Book b = new Book();

        b.setTitle("The Last Refuge");
        b.setAuthor("Craig Robertson");
        b.setBookID("1471127753");
        b.setonLoan(false);
        b.setNumberofLoans(2);

        Book c = new Book();

        c.setTitle("The Bird That Did Not Sing");
        c.setAuthor("Alex Gray");
        c.setBookID("0751548278");
        c.setonLoan(true);
        c.setNumberofLoans(7);

        ArrayList<Book> BookList = new ArrayList<Book>();
        BookList.add(a);
        BookList.add(b);
        BookList.add(c);





    Scanner SC = new Scanner(System.in);

    int Choice1;

       Choice1 = SC.nextInt();

       SC.close();


        switch (Choice1) {

        case 1:

            Scanner JK = new Scanner(System.in);

          System.out.println("'Add a book' selected");
          System.out.println(" ");


          break;

        case 2:
            System.out.println("'Edit a book's details' selected");
            System.out.println("Which Book would you like to edit?");
            System.out.println("");
            break;

        case 3:
            System.out.println("'Delete a book' selected");
            break;

        case 4:
              System.out.println("'Loan a book' selected");
              break;

        case 5:
              System.out.println("'Return a book' selected");
              break;

        case 6:
              System.out.println("Goodbye!");
              System.exit(0);

              break;

        case 7:
              System.out.println("Displaying Books");
              System.out.println("");
              Book.showBooks(BookList);
              System.out.println("");

              break;      

        default:
          System.out.println("Invalid selection. Try again");


        }
      }
      }

可根据要求将其他编码编辑到此问题中。

一个额外的问题是:在此之后如何编辑 Array 中 Book 对象的状态?

提前谢谢你。

【问题讨论】:

标签: java arraylist switch-statement


【解决方案1】:

只需使用 foreach 语句

for (Book book : bookList){
    System.out.println(book.getTitle());
}

这个想法是,对于您在案例语句中调用的每个方法,您的程序将遍历书籍列表,以显示它们或允许用户修改它们

我认为这与您尝试做的相反,据我了解,这是遍历列表并显示每本书的所有选项。反其道而行之

【讨论】:

    【解决方案2】:

    遗憾的是,switch case 只接受编译时已知常量的值。 一般来说,如果编译器在你的 switch 语句中为每个 case 找到了不错的常量,他们会为你构建一个 switch case 跳转表。 请注意,switch 仅适用于整数原始类型和枚举,因此如果您使用其他对象类型,则需要使用 if/else 语句。

    【讨论】:

      【解决方案3】:

      为什么要使用开关盒?

      获取用户输入,例如 bookId 并在列表中搜索该对象,然后编辑该对象的详细信息。

      步骤; 1.选择编辑书 2. 求书名 3. 从数组列表中获取 id 的 book

      List<Book> books = new ArrayList<Book>();
      
      // Populate list
      
      private Book getBookById(int id) {
          Iterator<Book> obj = books.iterator();
          while(obj.hasNext()) {
              Book book = obj.next();
              if(book.id == id)
                  return book;
          }
          return null;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多