課程
/后端開發
/Java
/Java入門第三季
圖書管理系統,有沒有寫的簡單點的答案?
2017-05-20
源自:Java入門第三季 1-9
正在回答
寫了個馬馬虎虎的異常鏈:
package stu.exrise.demo;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Library {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
?Library lib=new Library();
? ? ? String books[][]={{"100","童話鎮"},{"101","托福單詞"},{"102","歷史百科全書"},{"103","JAVA基礎教程"},{"104","游戲大全"}};
? ? ? Scanner scan=new Scanner(System.in);
? ? ? String num;
? ? ? String name;
? ? ? int a=1;
? ? ? int b=1;
? ? ? int c=1;
? ? ? System.out.println("歡迎進入圖書借書系統!");
? ? ? while(a!=0){
? ? ? ?a=0;
? ? ? System.out.println("請選擇查找圖書的方式:1 書名查找 ?2圖書序號查找");
? ? ? int n=scan.nextInt();
? ? ? int flag=-1;
? ? ? switch(n){
? ? ? case 1:
? ? ?while(b!=0){ ? ? ?
? ? ?b=0;
? ? ?System.out.println("請輸入要查找的書名:");
? ? ?name=scan.next();
? ? ?for(int i=0;i<books.length;i++){
? ? ? for(int j=0;j<books[i].length;j++){
? ? ?
? ? ? if(books[i][j].equals(name)){
? ? ? System.out.println("books:"+books[i][j]+"圖書序號為:"+books[i][j-1]);
? ? ? flag=1;
? ? ? break;
? ? ?} ?
? ? ? }
? ? ? if(flag==1){
? ? ?}
? ? ?if(flag!=1){
? ? ?lib.testHandle2();
? ? ?b++;
? ? ?break;
? ? ? case 2:
? ? ?while(c!=0){
?c=0;
?System.out.println("請輸入要查找書的序號:");
?num=scan.next();
?for(int i=0;i<books.length;i++){
? ? ? if(books[i][j].equals(num)){
? ? ? System.out.println("books:"+books[i][j+1]+"圖書序號為:"+books[i][j]);
? ? ??
?if(flag!=1){
? ? ?c++;
?}
? ? ? default:
? ? ?lib.testHandle1();
? ? ?a++;
? ? ?continue;
}
public void test1() throws BookException{
? throw new BookException("輸入命名錯誤,請按要求的重新輸入");
public void test2() throws BookException{
? throw new BookException("圖書不存在,請重新輸入");
public void testHandle1() throws BookException{
try{
test1();
}catch(BookException e){
//e.getCause();
//System.out.println(e);
};
public void testHandle2() throws BookException{
test2();
//throw e;
/*
?* 定義異常
?*/
class BookException extends Exception{
BookException(){};
BookException(String message){
System.out.println(message);
求大神,有沒有用異常鏈寫的,或者寫的簡單些的?
寫這段代碼的時候只有上帝和我知道什么意思。??現在,只有上帝知道。 package?com.imooc; import?java.util.Scanner; public?class?Test?{ public?static?void?main(String[]?args)?{ String[]?s?=?{"唐詩300首","格林童話","java編程思想","計算機操作系統"}; System.out.println("歡迎來到圖書管理系統"); while(true)?{ int?number?=?0; while(true) { System.out.println("選擇菜單?:輸入1:進入書名查找模式???輸入2:進入圖書序列號查找模式"); Scanner?sc?=?new?Scanner(System.in); try{ ?number?=?sc.nextInt(); }catch(Exception?a)?{ System.out.println("錯誤命令異常,請重新輸入"); sc?=?null; continue; } if(number?>=1?&&?number?<=2)?{ break; }else?{ System.out.println("沒有該選項!請重新輸入"); } } if(number?==?1)?{ while(true){ System.out.print("請輸入要查找的書名:"); Scanner?sc2?=?new?Scanner(System.in); String?book?=?null; try{ ?book?=?sc2.nextLine(); }catch(Exception?b)?{ System.out.println("錯誤命令異常,請重新輸入!"); sc2?=?null; continue; } boolean?b?=?true; for(int?x=0;x<s.length;x++)?{ if(s[x].equals(book))?{ System.out.println(s[x]+"這本書存在!"); b?=?false; break; } } if(b)?{ System.out.println(book+"這本書不存在"); System.out.println("是否重新輸入:是/否"); Scanner?sc5?=?new??Scanner(System.in); if(sc5.nextLine().equals("是"))?{ continue; } } break; } }else?if(number?==?2)?{ while(true)?{ System.out.print("請輸入要查找的序列號:"); int?y; Scanner?sc4?=?new?Scanner(System.in); try{ ?y?=?sc4.nextInt(); }catch(Exception?c)?{ System.out.println("錯誤命令異常,請重新輸入"); continue; } boolean?flag?=?false; for(int?x=0;x<s.length;x++)?{ try{ if(s[x].equals(s[y]))?{ System.out.println("序列號"+y+"為"+s[x]+"這本書存在!"); System.out.println("是否繼續查詢:是/否"); Scanner?sc6?=?new??Scanner(System.in); String?choose3?=?sc6.nextLine(); if(choose3.equals("是"))?{ break; }else?if(choose3.equals("否")){ flag?=?true; } break; } }catch(Exception?e)?{ System.out.println("圖書不存在異常!"); System.out.println("是否重新輸入:是/否"); Scanner?sc5?=?new??Scanner(System.in); String?choose2?=?sc5.nextLine(); if(choose2.equals("是"))?{ break; }else?if(choose2.equals("否")){ flag?=?true; break; } } } if(flag)?{ break; } } } Scanner?i?=?new?Scanner(System.in); String?choose?=?null; while(true)?{ System.out.println("是否返回選擇菜單??是/否"); try{ choose?=?i.nextLine(); break; }catch(Exception?e)?{ System.out.println("輸入的數值異常,請重新輸入"); i?=?null; continue; } } if(!choose.equals("是"))?{ break; } } } }
慕數據3565510 提問者
尷尬,沒有。。。。
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
1 回答圖書管理系統,有沒有寫的好的答案?
2 回答圖書管理系統
6 回答關于管理系統的
3 回答1-9“簡單借書系統”,求大神指導。
1 回答有沒有這道題用異常處理的答案?。?!
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-07-01
寫了個馬馬虎虎的異常鏈:
package stu.exrise.demo;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Library {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
?Library lib=new Library();
? ? ? String books[][]={{"100","童話鎮"},{"101","托福單詞"},{"102","歷史百科全書"},{"103","JAVA基礎教程"},{"104","游戲大全"}};
? ? ? Scanner scan=new Scanner(System.in);
? ? ? String num;
? ? ? String name;
? ? ? int a=1;
? ? ? int b=1;
? ? ? int c=1;
? ? ? System.out.println("歡迎進入圖書借書系統!");
? ? ? while(a!=0){
? ? ? ?a=0;
? ? ? System.out.println("請選擇查找圖書的方式:1 書名查找 ?2圖書序號查找");
? ? ? int n=scan.nextInt();
? ? ? int flag=-1;
? ? ? switch(n){
? ? ? case 1:
? ? ?while(b!=0){ ? ? ?
? ? ?b=0;
? ? ?System.out.println("請輸入要查找的書名:");
? ? ?name=scan.next();
? ? ?for(int i=0;i<books.length;i++){
? ? ? for(int j=0;j<books[i].length;j++){
? ? ?
? ? ? if(books[i][j].equals(name)){
? ? ? System.out.println("books:"+books[i][j]+"圖書序號為:"+books[i][j-1]);
? ? ? flag=1;
? ? ? break;
? ? ?} ?
? ? ? }
? ? ? if(flag==1){
? ? ? break;
? ? ? }
? ? ?}
? ? ?if(flag!=1){
? ? ?lib.testHandle2();
? ? ?b++;
? ? ?}
? ? ?
? ? ?}
? ? ?break;
? ? ? case 2:
? ? ?while(c!=0){
?c=0;
?System.out.println("請輸入要查找書的序號:");
?num=scan.next();
?for(int i=0;i<books.length;i++){
? ? ? for(int j=0;j<books[i].length;j++){
? ? ? if(books[i][j].equals(num)){
? ? ? System.out.println("books:"+books[i][j+1]+"圖書序號為:"+books[i][j]);
? ? ? flag=1;
? ? ? break;
? ? ?} ?
? ? ? }
? ? ? if(flag==1){
? ? ? break;
? ? ? }
? ? ??
? ? ?}
?if(flag!=1){
? ? ?lib.testHandle2();
? ? ?c++;
? ? ?}
?}
? ? ?break;
? ? ? default:
? ? ?lib.testHandle1();
? ? ?a++;
? ? ?continue;
? ? ?
? ? ? }
? ? ??
? ? ? }
}
public void test1() throws BookException{
? throw new BookException("輸入命名錯誤,請按要求的重新輸入");
}
public void test2() throws BookException{
? throw new BookException("圖書不存在,請重新輸入");
}
public void testHandle1() throws BookException{
try{
test1();
}catch(BookException e){
//e.getCause();
//System.out.println(e);
};
}
public void testHandle2() throws BookException{
try{
test2();
}catch(BookException e){
//throw e;
};
}
}
/*
?* 定義異常
?*/
class BookException extends Exception{
BookException(){};
BookException(String message){
System.out.println(message);
};
}
2017-05-21
求大神,有沒有用異常鏈寫的,或者寫的簡單些的?
2017-05-20
2017-05-20
尷尬,沒有。。。。