我想问一下,输入错误之后怎样才能让用户重新输入呢?
package com.jersey; import java.util.*; import javax.sql.rowset.spi.SyncFactoryException; public class LibSystem { static Book[] books={new Book("论语","000001"),new Book("老子","000002"),new Book("java编程思想","000003")}; public static void main(String[] args){ System.out.println("欢迎来到iLibrary图书检索系统!"); Scanner input=new Scanner(System.in); System.out.println("请选择:1.按书名检索 2.按编号检索 3.退出系统"); int n=input.nextInt(); switch(n){ case 1: System.out.println("请输入您所要检索的图书名称"); String a=input.next(); try{ String result=SearchByName(a); System.out.println("您所选的《"+result+"》已找到"); }catch(Exception e){ e.printStackTrace(); System.out.println("您检索的图书不存在!"); } break; case 2: System.out.println("请输入您所要检索的图书编码"); String b=input.next(); try{ String result2=SearchByCode(b); System.out.println("您所选的《"+result2+"》已找到"); }catch(Exception e){ e.printStackTrace(); System.out.println("您输入的编码有误,请重新输入!"); } break; case 3: System.exit(0); default: System.out.println("您的输入有误!请重新输入!"); } } public static String SearchByName(String a) throws Exception{ for(int i=0;i<books.length;i++){ if(a.equals(books[i].name)){ continue; } else{ throw new Exception(); } } return a; } public static String SearchByCode(String b) throws Exception{ String s2=b; for(int i=0;i<books.length;i++){ if(b.equals(books[i].code)){ s2=books[i].name; break; } else{ throw new Exception(); } } return s2; } }
诸位还有什么改进的建议也请告诉我,谢谢!