心中的愤怒就像龙咆哮
2016-07-17 14:07:06浏览 4475
package com.project3;
public class Book {
String [] book ={"语文","数学","英语","生物","物理","化学"};
}
package com.project3;
import java.util.Scanner;
public class bookSystem {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎来到借书系统O~O");
System.out.println("图书列表:");
System.out.println("序号"+"\t"+"书名");
Book bk = new Book();
int i = 0;
for(String BK : bk.book){
System.out.println(i+"\t"+BK);
i++;
}
for(;;){
try{
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
int input = Input();
switch(input){
case -1: System.out.println("命令输入错误!请输入int数据类型!");
continue;
case 1: String name = getbyname(bk.book);
System.out.println("book:"+name);
in.close();
break;
case 2: String name2 = getbynum(bk.book);
System.out.println("book:"+name2);
in.close();
break;
default: System.out.println("命令输入错误!请按照提示输入命令!");
continue;
}
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
}
}
public static String getbyname(String[] book) throws Exception{
for(;;){
try{
System.out.print("请输入书名:");
String input1 = in.next();
for(String b:book){
if(input1.equals(b)){
return b;
}
}
throw new Exception();
}catch(Exception e){
System.out.println("图书不存在!");
}
}
}
public static String getbynum(String[] book)throws Exception{
for(;;){
try{
System.out.print("请输入序号:");
int input2 = Input();
if(input2 == -1){
System.out.println("命令输入错误!请输入int数据类型!");
continue;
}else if(0<=input2&&input2<book.length){
return book[input2];
}
throw new Exception();
}catch(Exception e){
System.out.println("图书不存在!");
}
}
}
public static int Input(){
try {
int input = in.nextInt();
return input;
} catch (Exception e) {
// TODO: handle exception
in = new Scanner(System.in);
return -1;
}
}
}
热门评论
你好,我想问一下,input 函数 中,是怎样判断然后让返回值等于-1 的?
如果我输入5,input函数中异常是怎样运行的呢?
for(String BK : bk.book)
语法不是直接for(String BK : bk)?
确定循环次数用for
不确定用while