java:输入n本书,有三种类型的书,分别为Novel,Computer,Poetry,都具有相同属性书名和价格,各自有完本时间,推荐阅读年龄,作者属性,输入格式为,对于每本书的输入格式“书的类型,书名,价格,独有属性,”,最后输入要查询的书的类型,按输入顺序依次输出所有该类型书的信息,输出格式为“书名: name,单价: price,独有属性: 独有属性”,该属性书不存在则输出“Don't have this kind of book”
时间: 2024-02-25 14:55:39 浏览: 99
好的,我可以帮你解决这个问题。这是一个简单的Java程序,可以实现你的要求:
```java
import java.util.*;
class Book {
private String type;
private String name;
private double price;
private String uniqueProperty;
public Book(String type, String name, double price, String uniqueProperty) {
this.type = type;
this.name = name;
this.price = price;
this.uniqueProperty = uniqueProperty;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getUniqueProperty() {
return uniqueProperty;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Book> books = new ArrayList<>();
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
String type = scanner.next();
String name = scanner.next();
double price = scanner.nextDouble();
String uniqueProperty = scanner.next();
books.add(new Book(type, name, price, uniqueProperty));
}
String queryType = scanner.next();
boolean found = false;
for (Book book : books) {
if (book.getType().equals(queryType)) {
System.out.println("书名: " + book.getName() + ",单价: " + book.getPrice() + ",独有属性: " + book.getUniqueProperty());
found = true;
}
}
if (!found) {
System.out.println("Don't have this kind of book");
}
}
}
```
这个程序定义了一个`Book`类,包含类型、书名、价格和独有属性等信息。然后,我们使用`Scanner`类从控制台读入书的信息,并存储在一个`List<Book>`中。接着,我们根据输入的查询类型,在`List<Book>`中查找所有符合条件的书,并输出它们的信息。如果没有找到任何符合条件的书,则输出`Don't have this kind of book`。
阅读全文