First of all, paste the program to complete the requirements of the function!
Next, post the main program! The code that the beginner writes ... Written in Java.
Packageexc;//Input Error Exception Public classErrorexceptionextendsException { Publicerrorexception () {System.out.println ("Command input Error! Please enter the number command as prompted! "); }; } Packageexc;//there is no exception to the book Public classNoexceptionextendsException {//two methods with parameter construction, the first one is ' book name does not exist ' exception Publicnoexception (String name) {System.out.println ("Book" +name+ "does not exist"); } //the second is the ' book number does not exist ' exception PublicNoexception (intnum) {System.out.println ("The book serial number for" +num+ "does not exist"); }} Packageexc;//the construction of the book Class Public classBook {String name; intnum; PublicBook () {}; //the construction method with the parameter is used to construct the book class with the name and serial number. PublicBook (String name,intnum) { This. name=name; This. num=num; } } Packageexc;ImportJava.util.Scanner; Public classTest {StaticTest qwer=NewTest (); Scanner Reader=NewScanner (system.in); //building an array of book classesBook[] books={NewBook ("High Number", 1), NewBook ("Chinese", 2), NewBook ("English", 3), NewBook ("Physics", 4), NewBook ("Chemistry", 5), NewBook ("Creature", 6) }; Public Static voidMain (string[] args)throwsnoexception {//TODO auto-generated Method StubQwer.welcome ();//Tips WelcomeQwer.xunhuan ();//Call the Loop function and run the program until the correct book is found when an error occurs } //the body of the program run Public voidInputwelcome (intAthrowserrorexception, noexception{if(a==1) {System.out.println ("Enter the name of the book");//using the name lookup methodString b=Reader.next (); Qwer.corrcetname (b);//Find out if a book exists with the name confirmation method }Else if(a==2) {System.out.println ("Enter the book Number");//use ordinal lookup method intC=Reader.nextint (); Qwer.corrcetnum (c);//use ordinal confirmation to find out if there is a corresponding book }Else { Throw NewErrorexception ();//throw an input error exception } } //skeleton Program, complete frame Public voidRetime ()throwsnoexception{intA=Reader.nextint (); Try{qwer.inputwelcome (a); } Catch(errorexception e) {//TODO auto-generated Catch blockQwer.welcome (); Qwer.retime (); } } //The loop function, when an exception occurs, catches an input error exception and continues execution Public voidXunhuan () {Try{qwer.retime (); } Catch(Noexception e) {//when you catch an input error exception, continue to execute the program and find the book again//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("Wrong input, please select Again"); Qwer.welcome (); Qwer.xunhuan (); } } //confirm function When finding the input sequence number Public voidCorrcetnum (intbthrowsnoexception{intI=0; //throws an input error exception if the input ordinal is greater than the array length or is a complex number if(b>books.length| | B<0) { Throw NewNoexception (b); } //iterate through the array to confirm the existence of this ordinal book for(; i < books.length; i++) { if(books[i].num==b) {System.out.println ("Book:" +books[i].name); System.out.println ("Congratulations on finding the books you need!" "); Break; }; } } //confirm function When looking for input name Public voidCorrcetname (String B)throwsnoexception{inti = 0; //iterate through the array to confirm the existence of this name book for(; I < books.length;i++) { if(Books[i].name.equals (b)) {System.out.println ("Book:" +books[i].name); System.out.println ("Congratulations on finding the books you need!" "); Break; }; } //when the entire program is traversed, or the input name book is not found, the thrown book does not have an exception if(i==books.length) {Throw NewNoexception (b); } } //Welcome Interface Public voidwelcome () {System.out.println ("Input command: 1-Find books by name; 2-Find names by ordinal"); }}
Hope you see the program can go run a run! Together for reference Exchange!
MU class study notes! (Complete Find Books applet ~)