亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么代碼會出現循環。。

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 回答

Main方法中,在執行完library.rentbook()方法后有繼續調用了library.list()方法。把library.list()語句注釋了,就不會循環了。

1 回復 有任何疑惑可以回復我~

個人建議把main函數放在最底下,看起來舒服,也好調試順序。

1 回復 有任何疑惑可以回復我~

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

你自己又調用了一次,當然會重復了

1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么代碼會出現循環。。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號