为什么代码会出现循环。。
package librarySystem;
import java.util.*;
public class librarySystem{
//定义异常
public class nobookException extends Exception{
public nobookException() {}
public nobookException(String message) {
super(message);
}
}
Scanner input=new Scanner(System.in);
String[] bookList= {"高数","线代","无机化学","有机化学"};
//main
public static void main(String[] args) {
librarySystem library=new librarySystem();
library.rentBook();
library.list();
}
//input
public int input() {
Scanner sc=new Scanner(System.in);
try {
int i=input.nextInt();
return i;
}catch(Exception e){
System.out.println("请重新输入一个整数!");
return -1;
}
}
//租书系统
public void rentBook() {
System.out.println("请问您是否需要租书?1.是\t2.否");
int num=input();
try {
switch(num) {
case 1: list(); break;
case 2: System.exit(0); break;
default : throw new nobookException("请输入1或2");
}
}catch(Exception e) {
System.out.println(e.getMessage());
rentBook();
}
}
//list
public void list() {
for(int i=0;i<bookList.length;i++) {
System.out.println(i+1+"."+bookList[i]);
}
System.out.println("请输入 1、按序号查书;2、按书名查书;3、返回上一级");
int num=input();
try{
switch(num) {
case 1:
bookNum();
break;
case 2:
bookName();
break;
case 3:
rentBook();
break;
case -1:
list();
break;
default:
throw new nobookException("请重新输入对应的整数");
}
}catch(nobookException e) {
System.out.println(e.getMessage());
list();
}
}
//bookNum
public void bookNum() {
System.out.println("请输入图书的序号:");
int num=input();
try{
if(num<0||num>bookList.length)
throw new nobookException("请输入正确的图书编号!");
else
{
System.out.println(bookList[num-1]);
}
}catch(Exception e) {
System.out.println(e.getMessage());
bookNum();
}
}
//bookName
public void bookName() {
Scanner sc = new Scanner(System.in);
try {
System.out.println("请输入书名:");
String book= sc.next();
boolean flag=false;
for (String i:bookList) {
if (book.equals(i)) {
flag=true;
System.out.println(i);
break;
}
}
if(flag=false) {
throw new nobookException("请输入正确的书名:");
}
}catch (nobookException e){
System.out.println(e.getMessage());
bookName();
}catch (Exception e){
e.printStackTrace();
bookName();
}
}
}