交作業(yè)了?。。?/h1>
package exception_demo;
import java.util.Scanner;
public class Book {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book bookObj = new Book();
bookObj.init();
}
public void init() {
String[] books = {"java","php","python","go"};
int type;
String BookName;
try {
type = searchType();
if(type == 1) {
BookName = searchBookName(books);
}else {
BookName = searchBookNum(books);
}
System.out.println("book:"+BookName);
}catch(BookException e) {
System.out.println(e.getMessage());
init();
}catch(Exception e) {
System.out.println(e.getMessage());
init();
}
}
//通過(guò)哪種類型選擇圖書(shū)
public int searchType() throws BookException {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.print("請(qǐng)輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
int type = input.nextInt();
if(type == 1 || type == 2) {
return type;
}else{
throw new BookException("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
}
}
//通過(guò)名稱查找
public String searchBookName(String[] books) throws Exception {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.println("請(qǐng)輸入圖書(shū)名稱:");
String BookName = input.next();
for(String book :? books) {
if(book.equals(BookName)) {
return BookName;
}
}
throw new Exception("圖書(shū)不存在!");
}
//通過(guò)序號(hào)查找
public String searchBookNum(String[] books) throws Exception {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");
int BookNum = input.nextInt();
if(BookNum < books.length && BookNum >= 0) {
return books[BookNum-1];
}
throw new Exception("圖書(shū)不存在!");
}
}
package exception_demo;
public class BookException extends Exception{
public BookException(String message) {
super(message);
}
}
package exception_demo;
import java.util.Scanner;
public class Book {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book bookObj = new Book();
bookObj.init();
}
public void init() {
String[] books = {"java","php","python","go"};
int type;
String BookName;
try {
type = searchType();
if(type == 1) {
BookName = searchBookName(books);
}else {
BookName = searchBookNum(books);
}
System.out.println("book:"+BookName);
}catch(BookException e) {
System.out.println(e.getMessage());
init();
}catch(Exception e) {
System.out.println(e.getMessage());
init();
}
}
//通過(guò)哪種類型選擇圖書(shū)
public int searchType() throws BookException {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.print("請(qǐng)輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
int type = input.nextInt();
if(type == 1 || type == 2) {
return type;
}else{
throw new BookException("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
}
}
//通過(guò)名稱查找
public String searchBookName(String[] books) throws Exception {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.println("請(qǐng)輸入圖書(shū)名稱:");
String BookName = input.next();
for(String book :? books) {
if(book.equals(BookName)) {
return BookName;
}
}
throw new Exception("圖書(shū)不存在!");
}
//通過(guò)序號(hào)查找
public String searchBookNum(String[] books) throws Exception {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");
int BookNum = input.nextInt();
if(BookNum < books.length && BookNum >= 0) {
return books[BookNum-1];
}
throw new Exception("圖書(shū)不存在!");
}
}
package exception_demo;
public class BookException extends Exception{
public BookException(String message) {
super(message);
}
}
2020-03-14
兄弟 我試了一下你的代碼,有個(gè)問(wèn)題
下面這段里,當(dāng)我int type輸入非數(shù)字時(shí),比如我控制臺(tái)打一個(gè)a,為什么沒(méi)有報(bào)InputMismatchException,你的代碼運(yùn)行結(jié)果是顯示null,然后重新開(kāi)始,這個(gè)我想不通啊
//通過(guò)哪種類型選擇圖書(shū)
public int searchType() throws BookException {
Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象
System.out.print("請(qǐng)輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
int type = input.nextInt();
if(type == 1 || type == 2) {
return type;
}else{
throw new BookException("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
}
}
2020-03-13
這個(gè)是不是沒(méi)法實(shí)現(xiàn)報(bào)錯(cuò)之后重新輸入?