import?java.util.Scanner;
//命令錯(cuò)誤異常
class?CommandException?extends?Exception
{
CommandException(String?mis)
{
super(mis);
}
}
//圖書不存在異常
class?NotExistException?extends?Exception
{
NotExistException(String?mis)
{
super(mis);
}
}
class?Books
{
private?String[]?bookName=new?String[]{"論語","高數(shù)","數(shù)據(jù)結(jié)構(gòu)","JAVA編程思想","囚徒健身"};
//按書名查找
public?String?nameSearch(String?name)?throws?NotExistException
{
for(String?str:bookName)
{
if(str.equals(name))
return?str;
}
throw?new?NotExistException("圖書不存在!");
}
//按ID查找
public?String?idSearch(int?id)?throws?NotExistException
{
if(id>bookName.length||id<=0)
throw?new?NotExistException("圖書不存在!");
return?bookName[id-1];
}
//運(yùn)行程序
public?void?search()?throws?CommandException,NotExistException
{
Scanner?input=new?Scanner(System.in);
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
int?n=input.nextInt();
switch(n)
{
case?1:
System.out.println("輸入圖書名稱:");
System.out.println("book:"+nameSearch(input.next()));
break;
case?2:
System.out.println("輸入圖書序號:");
System.out.println("book:"+idSearch(input.nextInt()));
break;
default:
throw?new?CommandException("命令輸入錯(cuò)誤!請根據(jù)提示輸入數(shù)字命令!");
}
}
}
public?class?Library
{
public?static?void?main(String[]?args)?//throws?CommandException,NotExistException
{
Books?b=new?Books();
while(true)
{
try
{
b.search();
}
catch(NotExistException?e)
{
System.out.println(e.getMessage());
continue;
}
catch(CommandException?e)
{
System.out.println(e.getMessage());
continue;
}
catch(Exception?e)
{
System.out.println("命令輸入錯(cuò)誤!請根據(jù)提示輸入數(shù)字命令!");
continue;
}
break;
}
}
}
2015-05-06
我想問下,main()方法中while循環(huán)中的break有什么作用呢?