完全不熟悉Java,我需要一些帮助来创建这个BookList程序:)我已经阅读了我的书,我完全糊涂了。我需要创建一个名为BookList的arraylist,我需要显示一个带有案例开关的菜单,它将添加一本书,编辑一本书,删除一本书并显示书籍。我的程序已启动,但它不能完全工作它应该显示一个菜单供用户选择,它将执行以下功能,但我收到所有Book构造函数的错误消息。我希望你们中的一些Java大师可以帮助一个新手!这是我目前在BookList类
中的内容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");
}
}
}
答案 0 :(得分:1)
以下代码似乎试图在main
方法中定义构造函数。
public static void main (String[] args){
[...]
public static Book()
{
int title;
}
Book a = new Book();
构造函数需要直接在类中。它也不应该有static
修饰符。看起来title
local应该是String
类型的字段(在任何方法之外)。
private String title;
public Book() {
}
public static void main(String[] args) {
[...]
Book a = new Book();
答案 1 :(得分:0)
Book类未正确声明。尝试将声明放在BookList类块之外。
public class BookList{
[...]
}
public class Book{
public void setTitle(String newTitle){
title = newTitle;
}
private int title;
}
请注意,Book的默认构造函数是隐含的,如果您想稍后使用它,则必须为标题添加getter。
答案 2 :(得分:0)
考虑到你提出的问题,我试图尽我所能。
import java.util.Scanner;
import java.util.ArrayList;
public class BookList {
public static void main (String[] args){
Scanner SC = new Scanner(System.in);
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);
int Choice1=1;
while(Choice1!=5)
{
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: ");
Choice1 = SC.nextInt();
switch (Choice1) {
case 1:
System.out.println("'Add a book' ");
System.out.println("Enter the name ");
Book holder = new Book(SC.next());
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("");
// I do not understand what you want me to do here
break;
case 3:
for(int i=0;i<BookList.size();i++){
System.out.println(i+1+") "+BookList.get(i));
}
System.out.println("Choose a book to delete: ");
BookList.remove(SC.nextInt()-1);
break;
case 4:
System.out.println("Display all books");
for(Book x:BookList)
System.out.println(x);
break;
case 5:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid selection. Try again");
}
}
}
}
public class Book{
String Title;
public Book()
{
Title="";
}
public Book(String bookTitle)
{
Title=bookTitle;
}
public void setTitle(String bookTitle)
{
Title=bookTitle;
}
public String toString(){
return "Book Name "+Title;
}
}