Java arraylist&开关/箱 [英] Java arraylist & switch/case

查看:77
本文介绍了Java arraylist&开关/箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全不熟悉Java,我需要一些帮助来创建这个BookList程序:)我已经阅读了我的书,我完全糊涂了。我需要创建一个名为BookList的arraylist,我需要显示一个带有案例开关的菜单,它将添加一本书,编辑一本书,删除一本书并显示书籍。我有我的程序启动但它不能完全工作它应该显示一个菜单供用户选择,它将执行以下功能,但我收到所有Book构造函数的错误消息(错误是语法错误,插入EnumBody完成BlockStatement)。我希望你们中的一些Java大师可以帮助一个新手!这是我目前在BookList课程中所拥有的内容





Completely new to Java and I need a little help creating this BookList program :) I have read through my book and im completely confused. I need to create a arraylist called BookList, I need to display a menu with a case switch that will add a book, edit a book, remove a book and display the books. I have my program started but it does not work completely It is supposed to display a menu for the user to select from and it will do the following functions but I get an error message with all the Book constructors(The error is Syntax Error, insert "EnumBody" to complete BlockStatement). I was hoping some of you Java masters could help a newbie out! This is what I have so far in my BookList class


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

public class BookList {

public static void main (String[] args){





System.out.println(" =======================================");
    System.out.println("|       1. Add a book                   |");
    System.out.println("|       2. Edit a book                  |");
    System.out.println("|       3. Delete a book                |");
    System.out.println("|       4. Display all books            |");
    System.out.println("|       5. Exit the program             |");
    System.out.println("|  *Type a number to make a selection*  |");
    System.out.println(" =======================================");
    System.out.println("");
    System.out.print("Selection: ");

    public static Book()

    {
    int title;
    }
    Book a = new Book();

    a.setTitle("Stranger");


    Book b = new Book();

    b.setTitle("SQL");


    Book c = new Book();

    c.setTitle("HTML");


    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("Display all books");
          break;



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

          break;


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


    }
  }





我的尝试:



我曾尝试创建一个Enum,但我不知道该把它放在里面。我甚至不确定我是否正确设置了它。我已经通过很多例子并试图重新创建但是我在一个摊位。



What I have tried:

Ive tried creating an Enum but I do not know what to put inside of it. Im not even sure I set it up correctly. Ive been through many examples and tried to recreate but im at a stall.

推荐答案

你已经把所有东西放在你的主方法中,所以它不会起作用。我建议你花一些时间研究 Java教程 [ ^ ]并完成示例代码。
You have put everything inside your main method so it is not going to work. I would suggest you spend some more time studying the The Java Tutorials[^] and working through the sample code.


这篇关于Java arraylist&amp;开关/箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆