为了账号安全,请及时绑定邮箱和手机立即绑定

如果输入2要根据序号,为什么这个i++是dead code?任意输入一个非0整数并回车,为什么程序没有一点反应?

https://img1.sycdn.imooc.com//5c9722a90001071d16000639.jpg

package com.imooc;

import java.util.Arrays;

import java.util.Scanner;


public class Library {

    static Library test=new Library();

    public static void main(String[] args) {

        test.Lib();

    }

    String[] book= {"高数","Java","C","Python","C#","C++","Visual Basic","AI"};


    public void Lib() {

        System.out.println("输入命令:1,按照名称查找图书;2,按照序号查找图书");

        Scanner input=new Scanner(System.in);

        int num=input.nextInt();

        try {

            if(num==1) {

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

                Scanner input2=new Scanner(System.in);

                String bookname=input2.next();

                boolean status=Arrays.toString(book).contains(bookname);

                if(status)

                    System.out.println("book:"+bookname);

                else 

                    throw new Error404Exception();

                }else if(num==2) {

                    System.out.println("请输入图书序号(从0到7)");

                    Scanner input3=new Scanner(System.in);

                    int bookNum=input3.nextInt();

                    for(int i=0;i<=7;i++) {

                        if(bookNum==i) 

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

                            break;

                    }

                }else{

                    throw new Error691Exception();

                }catch(Error404Exception e) {

                    System.out.println("错误404:抱歉,该图书不存在,请重新输入");

                    test.Lib();

                }catch(Error691Exception e) {

                    System.out.println("错误691:输入错误,请重新输入");

                    test.Lib();

                }


    }

}


正在回答

2 回答

 if(bookNum==i) 

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

                            break;

这里 要加括号 不然只执行一次就break了


1 回复 有任何疑惑可以回复我~

for(int i=0;i<=7;i++) {

                        if(bookNum==i) 

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

                            break;

                    }

这一大段没必要,直接这样:

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

如果输入2要根据序号,为什么这个i++是dead code?任意输入一个非0整数并回车,为什么程序没有一点反应?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信