最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

初學(xué)異常處理,希望和大家互相交流學(xué)習(xí)

********

自定義異常類:


package com.Bookrent;


public class ExtraException extends Exception{


}

********

Bookrent類:


package com.Bookrent;


public class Book {

private String bookName;

private int bookNum;

public Book(String bookName,int bookNum)

{

this.bookName = bookName;

this.bookNum = bookNum;

}

public String getBookName() {

return bookName;

}

public void setBookName(String bookName) {

this.bookName = bookName;

}

public int getBookNum() {

return bookNum;

}

public void setBookNum(int bookNum) {

this.bookNum = bookNum;

}

}

********

主函數(shù):


package com.Bookrent;

import java.util.Scanner;


public class bookrent {

Book[] bookRent =?

{new Book("高數(shù)",1),new Book("數(shù)據(jù)結(jié)構(gòu)",2),new Book("操作系統(tǒng)",3),new Book("數(shù)據(jù)庫(kù)",4)};?

//將數(shù)組定義到全局范圍方便其他函數(shù)使用;

public static void main(String[] args) {

// TODO Auto-generated method stub

bookrent br = new bookrent();

br.find();

}


public void find()

{

Scanner input = new Scanner(System.in);

System.out.println("輸入命令:1.按照名稱查找圖書(shū);2.按照序號(hào)查找圖書(shū)");

int i = input.nextInt();

try

{

if(i == 1)

{

this.searchName();

}

else if(i == 2)

{

this.searchNum();

}

else?

{

throw ?new Exception();

}

}catch(Exception e)

{

input = new Scanner(System.in);//對(duì)于輸入有誤的情況不會(huì)終止程序運(yùn)行;

System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入正確的命令!");

this.find();

}

}

public void searchName()//對(duì)于輸入的信息進(jìn)行判斷并進(jìn)行有效的處理(try catch)異常處理并循環(huán)

{

System.out.println("請(qǐng)輸入要查詢的書(shū)名:");

Scanner input = new Scanner(System.in);

String s = input.next();

try

{

for(Book br:bookRent)//遍歷數(shù)組

{

if(br.getBookName().equals(s))//查看是否有何輸入相同的書(shū)名

{

System.out.println("已找到圖書(shū)");

System.out.println("圖書(shū):" + s);

this.find();//遍歷后要及時(shí)退出否則會(huì)強(qiáng)制拋出異常;

}

else

{

throw new Exception();

}

}

}catch(Exception e)

{

System.out.println("查找的圖書(shū)不存在");

this.find();

}


}

public void searchNum()

{

System.out.println("請(qǐng)輸入要查詢的書(shū)的序號(hào):");

Scanner input = new Scanner(System.in);

try

{

int i = input.nextInt();

for(Book br:bookRent)

{

if(br.getBookNum() == i)

{

System.out.println("已找到圖書(shū)");

System.out.println("圖書(shū):" + i);

this.find();

}

else

{

throw new ExtraException();//自定義異常的使用;對(duì)于多catch很有幫助;

}

}

}catch(ExtraException e)

{

System.out.println("查找的圖書(shū)不存在");

this.find();

}catch(Exception e)

{

System.out.println("命令輸入有誤,請(qǐng)重新輸入!");

this.searchNum();

}

}

}


正在回答

1 回答

可以,厲害

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

初學(xué)異常處理,希望和大家互相交流學(xué)習(xí)

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)