星葱
2015-02-08 13:29
package booksystem; import java.util.*; public class BookSystem { String book[]={"平凡的世界","高等数学","C程序设计"}; Scanner input=new Scanner(System.in); public static void main(String[] args) { BookSystem begin=new BookSystem(); begin.welcome(); } public void welcome(){ System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书;"); try{ int select=input.nextInt(); if(select==1) this.searchName(); else if(select==2) this.searchNum(); else throw new Exception(); }catch(Exception e){ System.out.println("请输入正确的指令"); input.nextLine(); //跳过错误类型 this.welcome(); } } public void searchName(){ System.out.println("请输入图书名称:"); String bookname=input.next(); System.out.println(bookname); Boolean flag=false; for(int i=0;i<book.length;i++) if(bookname.equals(book[i])) flag=true; if(flag) System.out.println("图书:"+bookname); else System.out.println("图书不存在"); this.welcome(); } public void searchNum(){ System.out.println("请输入图书序号:"); int num=input.nextInt(); if(num<=book.length) System.out.println("图书:"+book[num-1]); else System.out.println("图书不存在"); this.welcome(); } }
同学,请问你有什么问题吗?还是要保存代码,如果是保存代码我们是有专门的笔记可以保存代码的!
嗯嗯到时候可以帮忙哈哈加油哈Comdy!!!^_^...nbsp;
三个方法能用继承来完成吗?
为什么只有这个方法要用跳过错误类型?
input.nextLine(); //跳过错误类型
小白问下,
for(int i=0;i<book.length;i++)
if(bookname.equals(book[i]))
flag=true;
这段代码当IF判断语句为真时,true赋值给flag,for循环是否应该跳出,不再执行
this.welcome是什么意思啊????????
Java入门第三季
409779 学习 · 4339 问题
相似问题