package com.imooc;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld hello = new HelloWorld();
hello.UserInterface();
}
public void UserInterface(){
Scanner input = new Scanner(System.in);
ArrayList<String> words = new ArrayList<String>();
words.add("论语");
words.add("高数");
words.add("线性代数");
words.add("数据结构");
try{
System.out.println("输入命令: 1——按照名称查找图书; 2——按照序号查找图书");
switch(input.nextInt()){
case 1:
this.searchBookByName(words);
break;
case 2:
this.searchBookByNumber(words);
break;
default:
break;
}
}catch(InputMismatchException e){
System.out.println("命令输入错误!请按照提示输入数字命令!");
}
finally{
input.close();
}
}
public void searchBookByName(ArrayList<String> words){
Scanner input = new Scanner(System.in);
try{
System.out.println("输入图书名称:");
String inputName = input.next();
if(words.contains(inputName)){
System.out.println("book:" + inputName);
}else{
System.out.println("图书不存在");
}
}catch(InputMismatchException e){
System.out.println("命令输入错误!请输入图书名称!");
}
finally{
input.close();
}
}
public void searchBookByNumber(ArrayList<String> words){
Scanner input = new Scanner(System.in);
try{
System.out.println("输入图书序号:");
System.out.println("book:" + words.get(input.nextInt()));
}catch(InputMismatchException e){
System.out.println("命令输入错误!请根据提示输入数字命令");
}catch(IndexOutOfBoundsException e){
System.out.println("图书不存在");
}
finally{
input.close();
}
}
}
點(diǎn)擊查看更多內(nèi)容
17人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦