java try-catch 新手求助
问题描述:
public static void main(String[] args) {
Scanner input1=new Scanner(System.in);
String[] books={"","军事","历史","建筑","文学","数学","计算机","小说","百科"};
label1:
while(true){
Chaintest t1=new Chaintest();
Chaintest t2=new Chaintest();
System.out.println("请选择查询方法:a.图书信息 b.图书序号");
String choose=input1.next();
if(choose .equals ( "a")==true ){
System.out.println("选择了a方法");
try {
t1.getBookname(books);
} catch (Exception e) {
e.printStackTrace();
}
private static String getBookname(String[] newBooks) throws Exception{
Scanner input2=new Scanner(System.in);//储存图书名字信息
label2:
while(true)
System.out.println("请输入书名:");
String choose2=input2.next();
int i=0;
if (newBooks [i] .contentEquals(choose2)==false)
throw new Exception("很抱歉,您所输入的书籍不存在。请查询其他书目");
else
System.out.println("您所需要的书籍为:"+newBooks[i]);
}
}
想请教一下。
我在main方法中创建一个一位数组(用于保存图书信息),需要用户输入书籍名称检测该图书是否存在数组中。如果我输入的图书名称在数组里找不到,想重新返回getBookname中输入,在catch块中应该添加什么语言呢?
感谢回答!
答
你只能这么写
bool needretry = try;
while (!needretry) {
try {
你的代码
needretry = false;
}
catch {
needretry = true;
}
}