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

大家帮忙看哪逻辑错了,通过书名只能检索到“高数”通过序号只能检索到1号书的名字

package com.imooc;

public class Book {
	String Name;
	int Number;
	public void setName(String Name){
		this.Name=Name;
	}
	public void setNumber(int Number){
		this.Number=Number;
	}
    public  String getName(){
		return Name;
	}
	public int getNumber(){
		return Number;
	}
	public  Book(int Number,String Name){
		this.setNumber(Number);
		this.setName(Name);
		
	}

}


package com.imooc;
import java.util.*;

public class Test {

	private static Scanner console = new Scanner(System.in);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		while(true){
		System.out.println("输入命令:1-按照名称查找图书  2-按照序号查找图书");
		Scanner input=new Scanner(System.in);
		
		int key=input.nextInt();  //选择查询方式
		Book[] lab={new Book(1,"高数" ),new Book(2,"数据结构"),new Book(3,"资源革命")};
		try{
		if(key==1){
			for(Book Book:lab){
			System.out.println("输入图书名称:");
			String nam = console.next();
			if(nam.equals(Book.getName())){
				System.out.println("书名:"+Book.getName());
			}
			else{
				throw new Exception("图书不存在!");
				
			}
			}
		}
		else if(key==2){
			for(Book Book:lab){
			System.out.println("输入图书序号:");
			int num=input.nextInt();
			if(num==(Book.getNumber())){
				System.out.println("书名:"+Book.getName());
			}
			else{
				throw new Exception("图书不存在!");
			}
			}
		}
		else{
			throw new Exception("命令输入错误!请根据提示输入数字命令!");
		}
		
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		}
	}
	
}



正在回答

3 回答

你那个foreach位置不对,应该放在输出后面,放在前面相当于for循环一样,按数组位置循环检查当前输入与该位置课程序号或名字是否匹配。还有我实在不知道怎样让未找到对应课程时报错,所以参照第一章课程演示代码定义了两个方法,即没找到课程就没有返回值就报错。

import java.util.*;


public class Test {


    private static Scanner console = new Scanner(System.in);

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

    public static void main(String[] args) {

        // TODO Auto-generated method stub

   

        while(true){

        Book[] lab={new Book(1,"高数" ),new Book(2,"数据结构"),new Book(3,"资源革命")};

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

        int key=input.nextInt();  //选择查询方式

        try{

        if(key==1){

        getBookByName(lab);

            }

   

        else if(key==2){

        getBookByNumber(lab);

        }

        else{

            throw new Exception("命令输入错误!请根据提示输入数字命令!");

        }       

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

        }

    }

    

    private static String getBookByName(Book[] lab)

throws Exception {

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

     String nam = console.next();

     for(Book Book:lab){

     if(nam.equals(Book.getName())){

         System.out.println("书名:"+Book.getName());

         return Book.getName();

     }

     }

//若无匹配,抛出”图书不存在异常“

throw new Exception("图书不存在!");

}

 

    private static String getBookByNumber(Book[] lab)

throws Exception {

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

    int num=input.nextInt();

        for(Book Book:lab){

        if(num==Book.getNumber()){

            System.out.println("书名:"+Book.getName());

            return Book.getName();

        }

        }

    //若无匹配,抛出”图书不存在异常“

    throw new Exception("图书不存在!");

     }

  

}


 class Book {

    String Name;

    int Number;

    public void setName(String Name){

        this.Name=Name;

    }

    public void setNumber(int Number){

        this.Number=Number;

    }

    public  String getName(){

        return Name;

    }

    public int getNumber(){

        return Number;

    }

    public  Book(int Number,String Name){

        this.setNumber(Number);

        this.setName(Name);

         

    }

 

}

 


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

叫啥都存在 提问者

懂了,非常感谢!
2016-03-12 回复 有任何疑惑可以回复我~

还是有些地方看不懂呀,如那个e.getMessage();出处何?


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

Book[] lab={new Book(1,"高数" ),new Book(2,"数据结构"),new Book(3,"资源革命")};

数组的创建出错了

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

举报

0/150
提交
取消

大家帮忙看哪逻辑错了,通过书名只能检索到“高数”通过序号只能检索到1号书的名字

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号