为什么代码会出现循环。。

来源:1-9 经验总结

qq_慕九州502170

2018-11-15 16:19

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();
        }
 }
 
}



写回答 关注

3回答

  • qq_慕慕7372986
    2018-11-16 19:14:15

    Main方法中,在执行完library.rentbook()方法后有继续调用了library.list()方法。把library.list()语句注释了,就不会循环了。

  • 慕码人4581752
    2018-11-16 14:41:39

    个人建议把main函数放在最底下,看起来舒服,也好调试顺序。

  • 慕码人4581752
    2018-11-16 14:40:01

    public static void main(String[] args) {
      librarySystem library=new librarySystem();
      library.rentBook();
      library.list();

    你自己又调用了一次,当然会重复了

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409777 学习 · 4339 问题

查看课程

相似问题