谁有很好的代码,方法用的都是try-catch,而不是都用if语句,我的代码不能按照名称查找,有什么问题

来源:1-9 经验总结

wl1224500

2015-03-15 12:48

谁有很好的代码供参考呀,我的代码输入序号还可以,但是按照名称查找就是输不出来,输出都是图书不存在

public class jieshu {

String[] book={"语文","数学","英语","物理","化学","生物"};

Scanner input=new Scanner(System.in);

public void test1(){

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

String name=input.next();

try {

for(int i=0;i<book.length;i++){

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

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

}else{

} throw new Exception();

}

}catch (Exception e) {

System.out.println("图书不存在异常");

}

}

public void test2(){

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

int number=input.nextInt();

try {

if(number<=book.length){

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

}else{

throw new Exception();

}

} catch (Exception e) {

System.out.println("图书不存在");

}

}

public void test(){

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

try {

int score=input.nextInt();

if(score==1){

this.test1();

}

if(score==2){

this.test2();

}else{

throw new Exception();

}

} catch (Exception e) {

System.out.println("命令输入错误,请根据提示输入!");

this.test();

}


}

public static void main(String[] args) {

jieshu a=new jieshu();

a.test();

}

}


写回答 关注

5回答

  • 夜枫丶
    2015-03-15 19:46:17
    已采纳

    public class Books {


    /**

    * 要求:

    * 1、定义字符串数组保存图书信息 

    * 2、提示用户输入,分别用"书名"和"图书序号"查找图书 

    * 3、根据输入信息进行适当的异常处理:

    * a、如果输入类型错误,抛出"错误命令异常",并提示重新输入 

    * b、如果书名不存在,抛出"图书不存在异常",并提示重新输入

    * c、如果图书序号超出字符串数组范围,抛出"图书不存在异常",并提示重新输入

    * @param args

    */

    //定义数组

    String[] book = new String[]{ "数学","语文","英语","历史","地理"};

    Scanner scan = new Scanner(System.in);//从控制台接受命令

    public static void main(String[] args){

    Books books = new Books();

    books.test();

    }

    /**

    * 创建test方法,用于选择查找方式

    */

    public void test(){

    System.out.println("请选择查找方式:1、书名\t 2、图书序号");

    try {

    Integer num = scan.nextInt();

    if (num == 1) {

    this.SearchName();

    } else if (num == 2) {

    this.SearchNum();

    }else{

    throw new Exception();//不匹配抛出异常

    }

    //捕获异常,输出异常,提示用户重新输入 ,并且调用test方法重新选择查找方式

    } catch (Exception e) {

    scan = new Scanner(System.in);

    System.out.println("错误命令! 请重新输入");

    this.test();

    }

    }

    /**

    *创建 SearchName方法,用于查找书名

    */

    public void SearchName(){

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

    String title = scan.next();//接收控制台输入的书名

    try {

    //定义一个j变量用于记录循环次数

    int j = 0;

    //for循环遍历数组

    for (int i = 0; i < book.length; i++) {

    if (title.equals(book[i])) {

    System.out.println("这是你要找的书籍:" + book[i]);

    break; 

    }

    j++;

    }

    //判断j变量是否等于数组长度,等于则抛出异常

    if (j == book.length) {

    throw new Exception();

    }

    //捕获异常,输出异常,提示用户重新输入 ,并且调用SearchName方法重新接收控制台输入的数据

    } catch (Exception e) {

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

    this.SearchName();

    }

    }

    /**

    *创建 SearchNum方法,用于查找数组中的图书序号

    */

    public void SearchNum(){

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

    //定义一个num变量用于接受控制台输入的数据

    int num = scan.nextInt();

    int j=0;

    try {

    //for循环遍历数组

    for (int i = 0; i < book.length; i++) {

    //判断num是否等于i,等于则输出数组中i的位置上的元素

    if (num == i) {

    System.out.println("这是你要找的书籍:" + book[i]);

    break;

    }

    //定义一个j变量用于记录循环次数

    j++;

    }

    //判断j变量是否等于数组长度,等于则抛出异常

    if(j == book.length){

    throw new Exception();

    }

    //捕获异常,输出异常,提示用户重新输入 ,并且调用SearchNum方法重新接收控制台输入的数据

    } catch (Exception e) {

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

    this.SearchNum();

    }

    }

    }



    这是我写的   你参考一下

    sun_ha...

    scan = new Scanner(System.in);为什么不写这句,调用test()会进入死循环?

    2015-08-02 14:06:37

    共 2 条回复 >

  • 李立帆
    2015-03-19 10:28:12
    Java第三季1-9经验总结-----源码

    package com.java;

    import java.util.InputMismatchException;

    import java.util.Scanner;


    public class LibrarySystem {


    /**1.定义字符串数组保存图书信息

    * 2.提示用户输入,分别按“书名”和“图书序号”查找图书

    * 3.根据输入信息进行适当的异常处理

    *   a、如果输入类型错误,抛出“错误命令异常”,并提示重新输入

    *   b、如果书名不存在,抛出“图书不存在异常”,并提示重新输入

    *   c、如果图书序号超过字符串数组范围,抛出“图书不存在异常”,

    *      并提示重新输入

    * @param newChoice 

    * @param args

    */

    static int choice;

    static int bookNum;//输入的图书序号

    static String bookName;//输入的图书名称

    //public static String[][] Book={{"数据结构","1"},{"软件设计","2"},{"高等数学","3"},{"网络安全","4"}};

    public static String[][] Book=new String[4][4];

    //static Scanner input=new Scanner(System.in);

    public static void main(String[] args) throws Exception {

           bookMaintenance();

           bookShow();

      for(;;){

      findBook();

      }

    }

    public static void findBook(){    

        choiceInput(choice);

            switch(choice){

            case 1:   

            bookNameInput();

            for(int i=0;i<Book[0].length;i++){

            if (bookName.equals(Book[0][i])){

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

            break;

            }

            else{

            if(i+1==(Book[0].length)){

               System.out.println("您查找的图书不存在!");

            }

            }

            }

               break;  

            case 2:

            bookNumInput(bookNum);

            for(int i=0;i<Book[1].length;i++){

            if (bookNum==Integer.parseInt(Book[1][i])){

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

            }

            else{

            if(i+1==Book[1].length){

               System.out.println("您查找的图书不存在!");

            }

            }

            }

               break; 

            }

    }


        public static int choiceInput(int newChoice){    

        boolean m = false;

    outer:

        do{  

    try{

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

        Scanner input=new Scanner(System.in);

        int temp=input.nextInt();

        m=isNumeric(Integer.toString(temp));

        if(m){

           newChoice=temp;

        }

    }catch(InputMismatchException e){

    System.out.println("命令输入错误!");

    if(newChoice==0){

    newChoice+=1;

    System.out.println("系统将默认按书名进行图书查找!");

        break outer;    

        }

    }finally{

    new LibrarySystem();

    }

        }while(!m&&(newChoice!=1|newChoice!=2));

    choice=newChoice;

    return choice;

    }


    public static void bookNameInput(){

    outer:

        do{

    try{

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

        Scanner input=new Scanner(System.in);

    bookName=input.next();

    if(bookName==null){

       continue outer;

    }

    }catch(Exception e){

            System.out.println("图书不存在异常!请输入正确的图书名称!"); 

            if(bookName==null){

       continue outer;

               }

    }

        }while(bookName==null);

        }

        public static int bookNumInput(int newbookNum){   

        boolean m = false;

        outer:

        do{  

        try{

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

            Scanner input=new Scanner(System.in);

        newbookNum=input.nextInt();    

        m=isNumeric(Integer.toString(newbookNum));

        if(newbookNum<=0||newbookNum>=Book[1].length){

    new LibrarySystem();

    continue outer;  

    }

    }catch(InputMismatchException e){

    System.out.println("命令输入错误!请根据提示重新输入数字命令!");

    if(m==false){

    new LibrarySystem();

    continue outer;    

        }

    }

        }while(!m&&(newbookNum<=0||newbookNum>=Book[1].length));

        bookNum=newbookNum;

        return bookNum;

        }

        public static boolean isNumeric(String str){

         if(str.matches("\\d *")){

          return true;

         }else{

          return false;

         }

        }

        

        public static void bookMaintenance(){

        Book[0][0]="数据结构";

        Book[0][1]="软件设计";

        Book[0][2]="高等数学";

        Book[0][3]="网络安全";

        Book[1][0]="1";

        Book[1][1]="2";

        Book[1][2]="3";

        Book[1][3]="4";

        }

        public static void bookShow(){

        System.out.println("序号         书名");

        System.out.println(" "+Book[1][0]+"   "+Book[0][0]);

        System.out.println(" "+Book[1][1]+"   "+Book[0][1]);

        System.out.println(" "+Book[1][2]+"   "+Book[0][2]);

        System.out.println(" "+Book[1][3]+"   "+Book[0][3]);

        }

    }

    Java第三季1-9经验总结-----源码

  • wl1224500
    2015-03-15 19:37:57

    public void searchName(){

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

    String name=input.next();

    try {

    int j=0;

    for(int i=0;i<book.length;i++){

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

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

    break;

    }

    j++;

    }

    if (j == book.length) {

    throw new Exception();

    }

    }catch (Exception e) {

    System.out.println("图书不存在异常");

    }

    }

    我试过了,还是不行,不过谢谢你!结果是:

    输入图书名称

    语文

    图书不存在异常

    命令输入错误,请根据提示输入!

    输入命令:1-按照名称查找图书;2-按照序号查找图书


  • 夜枫丶
    2015-03-15 19:05:06

    try {

    //定义一个j变量用于记录循环次数

    int j = 0;

    //for循环遍历数组

    for (int i = 0; i < book.length; i++) {

    if (title.equals(book[i])) {

    System.out.println("这是你要找的书籍:" + title);

    //用break语句跳出循环

    break;  

    }

    j++;

    }

    //判断j变量是否等于数组长度,等于则抛出异常

    if (j == book.length) {

    throw new Exception();

    }

    //捕获异常,输出异常,提示用户重新输入 ,并且调用SearchName方法重新接收控制台输入的数据

    } catch (Exception e) {

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

    this.SearchName();

    }


    wl1224...

    我试过了,还是不行,不过谢谢你!结果是: 输入图书名称 语文 图书不存在异常 命令输入错误,请根据提示输入! 输入命令:1-按照名称查找图书;2-按照序号查找图书

    2015-03-15 19:39:04

    共 1 条回复 >

  • 夜枫丶
    2015-03-15 13:08:18

    因为你那for循环那里只循环了一次  也就是说  当你输入 数学 时  第一次循环i=0,if判断name和book[0]的元素不匹配,它就直接进入了else中的代码块,也就是抛出异常!  所以你这按照书名查找的代码最多能只能输出 语文 这个元素!

    天蓝色的彼岸... 回复wl1224...

    在控制台输入中文时,我们只能从刚刚写的代码中将需要输入的书籍名称进行复制,粘贴到控制台中才可以~~~

    2015-05-04 16:03:22

    共 3 条回复 >

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409784 学习 · 4339 问题

查看课程

相似问题