package rentbook;import java.util.Scanner;public class text {?? public static Scanner scan=new Scanner(System.in);?? public static book[] bo={??????????? new book("高數(shù)"),??????????? new book("物理"),??????????? new book("語文"),??????????? new book("英語"),??????????? new book("c語言")?? ???? };?? ???? public static void main(String[] args) {??????? // TODO Auto-generated method stub????? while (true){????????? System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");????????? int inputnum=inputnum1();????????? switch(inputnum){????????? case 1 : System.out.println("按照名稱查找圖書");?????????????????? inputname();?????????????????? break;????????? case 2: System.out.println("按照序號查找圖書");????????????????? System.out.println("請輸入序號");????????????????????? try{????????????????????????? int putnum=inputnum1();????????????????????????? switch(putnum){????????????????????????? case -1: System.out.println("請輸入數(shù)字");break;????????????????????????? default:System.out.println(bo[putnum-1]+"已經(jīng)被您選中了");break;????????????????????????? }????????????????????? }catch(Exception e){????????????????????????? System.out.println("您輸入的數(shù)字過大");????????????????????? }????????????????????? break;????????? case -1 : System.out.println("請輸入數(shù)字");break;????????? default: System.out.println("輸入的數(shù)字不存在");????????? }????? }??? }??? private static void inputname() {??????? // TODO Auto-generated method stub??????? System.out.println("請輸入書名");??????? String input=scan.next();??????????????? for(int i=0;i<bo.length;i++){??????????????? if(input.equals(bo[i].name))??????????????? System.out.println(bo[i].name+"已經(jīng)被您選中了");??????????????? else if(i==bo.length-1){?????????????? ??? ?System.out.println("請輸入正確的書名");?????????????? ??? ?break;??????????????? }??????????????? }??? }??? private static int inputnum1() {??????? // TODO Auto-generated method stub????? ???????? try{?????? ??? ?int a=scan.nextInt();??????????? return a;??????? }catch(Exception e){????????????? return -1;??????? }??? }}
為什么在第一次詢問的時候輸入-1的時候程序正常 而輸入字符卻死循環(huán)?
qq_什么亡魂溺海沒有跳海實在_03283094
2016-11-29 21:33:07