大神帮帮忙,斗龙战士为什么不能玩玩

404 - 找不到文件或目录。
404 - 找不到文件或目录。
您要查找的资源可能已被删除,已更改名称或者暂时不可用。请大神帮忙看一下,为什么else if里面使用break不能跳出while(true)循环
package java4;import java.util.InputMismatchEimport java.util.Spublic class Books { //定义字符串数组用于保存图书信息 //在static方法外部定义的属性,要想在static类型的方法中调用,则要加上"static" static String[] books={"高数","大学英语","数据结构","JAVA入门","软件工程","大学物理","数据库"}; public static void main(String[] fargs) {
//创建Scannner对象用于用户输入
Scanner input=new Scanner(System.in);
//创建死循环while(true)用于保证系统运行
stuu: while(true){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
//取得整型命令
int a=input.nextInt();
System.out.println("输入图书名称:");
//用户输入图书名称
String name=input.next();
//for循环遍历所有图书
for(int i=0;i&books.i++){
//books[i]==两个引用之间可以用等于来进行比较是否相等
if(books[i].equals(name)){
System.out.println("book: "+books[i]);
//跳出for循环
}else if(i==books.length){
throw new RuntimeException("图书不存在!");
}else if(a==2){
System.out.println("输入图书序号:");
//用户输入图书序号
int number=input.nextInt();
//输出查找到的图书信息
System.out.println("book:"+books[number]);
//跳出死循环
//有问题,break不能用在try--catch语句中!!!
throw new Exception();
}catch (InputMismatchException e) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
//进行数据回滚
main(null);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在");
main(null);
}catch(RuntimeException e){
System.out.println(e.getMessage());
main(null);
}catch(Exception e){
System.out.println("命令输入错误!请根据提示输入数字命令!");
main(null);
写下你的评论...
break跳出的是外部的while()循环啊,跟有没有if语句没关系吧
break跳出当前循环,如果要跳出外部循环需要使用标记。
写下你的评论...
Copyright (C) 2018 imooc.com All Rights Reserved | 京ICP备 号-11

我要回帖

更多关于 玩吧为什么吹牛不能玩 的文章

 

随机推荐