ITPub博客

首页 > 应用开发 > > 为什么加了个while(bl)和try catch就可以重新输了?

为什么加了个while(bl)和try catch就可以重新输了?

作者:gamebus 时间:2019-07-09 10:29:51 0 删除 编辑

为什么加了个while(bl)和try catch就可以重新输了?输入的时候图书名称打错怎么没有catch 后面那句话啊?package com.imooc;


import java.util.Scanner;

public class Book {

static String[] name={new String("大学"),new String("中庸"),

new String("论语"),new String("孟子")};

static int[] num={1,2,3,4};

public static void main(String[] args) {

System.out.println("1-按照名称查找图书;2-按照序号查找图书");

Scanner input=new Scanner(System.in);

int score=input.nextInt();

boolean bl=true;

while(bl){

try{

while(score==1){

System.out.println("输入图书名称");

String bookname = input.next();

int i=0;

if(bookname.equals(name[i])){

System.out.println("book:"+name[i]);

bl=false;

i++;

break;

}

}

while(score==2){

System.out.println("输入图书序号");

int booknum=input.nextInt();

if(booknum==num[booknum-1]){

System.out.println("book:"+name[booknum-1]);

bl=false;

break;

}

}

}catch(Exception e){

System.out.println("图书不存在,请重新输入");

}

}

}

}




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4687/viewspace-3151428/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    155
  • 访问量
    13178