为什么return报错?
public static String Bookname(String[] book){//报错This method must return a result of type String
String name=input.nextLine();
for(int i=0;i<book.length;i++ ){
if(name.equals(book[i])){
return book[i];
}
而下面这个却没有报?
private static String getBookByName(String[] books)
throws Exception {
System.out.println("输入图书名称:");
//获取输入的图书名称
String name = console.next();
for (int i = 0; i < books.length; i++) {
if (name.equals(books[i]))
//输入的名称与某一图书名称匹配,返回该图书
return books[i];
}
//若无匹配,抛出”图书不存在异常“
throw new Exception("图书不存在!");
}