温凉a
2018-10-07 10:46
package book_retrieval_system;
import java.util.Scanner;
public class RetrievalSystem {
String[] books = {"Head First Java","Java数据结构","Java从入门到精通"};
public static void main(String[] args) {
// TODO Auto-generated method stub
RetrievalSystem find = new RetrievalSystem();
find.books();
}
public void books(){
try {
System.out.println("请选择查找方式:1、以书名查找。2、以图书序号查找。");
Scanner inputWays = new Scanner (System.in);
int way = inputWays.nextInt();
if(way==1||way==2) {
ways(way);
}else {
System.out.println("命令输入错误,请输入1或2");
books();
}
}catch(Exception ex) {
System.out.println("命令输入错误,请输入1或2");
books();
}
}
public void ways(int way) {
if(way==1) {
Scanner input1 = new Scanner(System.in);
System.out.println("请输入书名:");
String bookName = input1.next();
if (contains(books,bookName)==true) {
System.out.println("您要找的图书是:" + bookName);
books();
}else {
System.out.println("图书不存在");
books();
}
}else if(way==2) {
Scanner input2 = new Scanner(System.in);
System.out.println("请输入序号:");
int bookNumber = input2.nextInt();
if(bookNumber<=books.length&&bookNumber>0) {
System.out.println("您要找的图书是:" + books[bookNumber-1]);
books();
}else {
System.out.println("您输入有误或图书不存在");
books();
}
}
}
private boolean contains(String[] books,String bookName) {
// TODO Auto-generated method stub
for(String find:books) {
if(find.equals(bookName))
return true;
}
return false;
}
}
不好意思啊,我也是想参考一下经验,不是应该自定义异常吗,你这里写的好像判断,
写的不错!
contains方法可以不写,直接遍历book,bookname.quals(book[i])也不错
小白求问:
if(way == 1 || way == 2){
ways(way);
这两句是啥意思
能不能写个注释 亲?
?????说好的排版呢
Java入门第三季
409792 学习 · 4340 问题
相似问题