用JAVA求 图书馆新进了一批书,每本书包括ISBN、书名、作者、类别,定义Book类,并用对象数组存储这些书,根据输入的类别,查询书,有多个案例,每个案例,先输入书的个数n,然后输入n本书的信息,再输入要查询的类别,输出根据输入的作者查询书每行代码注释一下
时间: 2024-06-13 14:08:35 浏览: 209
运用JAVA图书查询系统
4星 · 用户满意度95%
```java
import java.util.Scanner;
public class Book {
private String ISBN;
private String name;
private String author;
private String category;
public Book(String ISBN, String name, String author, String category) {
this.ISBN = ISBN;
this.name = name;
this.author = author;
this.category = category;
}
public String getCategory() {
return category;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入书的个数:");
int n = scanner.nextInt();
Book[] books = new Book[n];
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i + 1) + "本书的信息:");
System.out.print("ISBN:");
String ISBN = scanner.next();
System.out.print("书名:");
String name = scanner.next();
System.out.print("作者:");
String author = scanner.next();
System.out.print("类别:");
String category = scanner.next();
books[i] = new Book(ISBN, name, author, category);
}
System.out.print("请输入要查询的类别:");
String queryCategory = scanner.next();
System.out.println("查询结果如下:");
for (Book book : books) {
if (book.getCategory().equals(queryCategory)) {
System.out.println(book.ISBN + " " + book.name + " " + book.author + " " + book.category);
}
}
}
}
```
--相关问题--:
1. 如何在Java中读取文件?
2. 如何在Java中
阅读全文