Java圖書查詢系統執行個體(簡化精鍊版及部分更正)

來源:互聯網
上載者:User

標籤:return   sharp   ati   建立   選擇   lis   throw   continue   except   

1.建立書大類

public class Book {private String name;private int num; public Book(String name,int num){this.name=name;this.num=num;}public String getName(){return name;}public void setName(String name){this.name=name;}public int getNum(){return num;}public void setNum(int num){this.num=num;}}

 2.自訂異常

public  class NoExistException extends Exception{public NoExistException(String message){super(message);}public NoExistException(){}}

 3.主程式

import java.util.*;public class Test2 { private Scanner input = new Scanner(System.in); Book[] book={ new Book("語文",1), new Book("數學",2), new Book("英語",3), new Book("專業課",4) };// public List<Book> listbooks;// public Test2() {//         this.listbooks = new ArrayList<Book>();//         }//     public void listBooksAdd() {//         listbooks.addAll(Arrays.asList(book));//         }  public void printbook(){ System.out.println("====歡迎使用選書系統===="); System.out.println("=====書本資訊如下:====="); System.out.println("序號"+"\t"+"書名"); for(Book bk:book){ System.out.println(bk.getNum()+"\t"+bk.getName()); } } public static void main(String[] args) {Test2 tc=new Test2();//tc.listBooksAdd();tc.printbook();while(true){    System.out.println("請選擇查書方式1-通過序號2-通過書名:");switch(tc.back()){case 1:try{ System.out.println("您的書為:"+tc.fbid()); break;}catch(Exception e){System.out.println(e.getMessage());continue;}case 2:try{System.out.println("您的書為:"+tc.fbbn());break;}catch(Exception e){System.out.println(e.getMessage());continue;}default:System.out.println("輸入錯誤!請重新輸入!!!");continue;}break;}}public String fbid() throws NoExistException{System.out.println("請輸入圖書的序號:");int in=input.nextInt();for(Book bk:book){if(in==bk.getNum()){return book[in-1].getName();}}throw new NoExistException("序號越界請重新輸入");}public String fbbn()throws NoExistException{System.out.println("請輸入圖書的書名:");String in=input.next();for(Book bk:book){if(in.equals(bk.getName())){return bk.getName();}}throw new NoExistException("此書不存在");}public int back(){int in=input.nextInt();return in;}}

 

Java圖書查詢系統執行個體(簡化精鍊版及部分更正)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.