package com.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class RentBook {
public static void main(String[] args) {
String[] books = {"高数","离散数学","数据结构","算法导论","数据库","JAVA语言"};
for(int i=0;;i++){
System.out.println("请选择查询方式:按照图书名称查询输入1,按照图书序号查询输入2");
Scanner scan = new Scanner(System.in);
int input = 0;
try{
input = scan.nextInt();
}catch(InputMismatchException e){
// e.printStackTrace();
System.out.println("输入命令错误,请输入数字1或2");
}
if(input==1){
System.out.println("请输入图书名称:");
try{
String input1 = scan.next();
switch(input1){
case "高数":
System.out.println("book:"+books[0]);
break;
case "离散数学":
System.out.println("book:"+books[1]);
break;
case "数据结构":
System.out.println("book:"+books[2]);
break;
case "算法导论":
System.out.println("book:"+books[3]);
break;
case "数据库":
System.out.println("book:"+books[4]);
break;
case "JAVA语言":
System.out.println("book:"+books[5]);
break;
default:
System.out.println("图书不存在,请选择其他书籍");
break;
}
}catch(InputMismatchException e){
System.out.println("输入命令错误,请输入图书名称");
}
}else{
System.out.println("请输入图书序号:");
try{
int input2 = scan.nextInt();
switch(input2){
case 1:
System.out.println("book:"+books[0]);
break;
case 2:
System.out.println("book:"+books[1]);
break;
case 3:
System.out.println("book:"+books[2]);
break;
case 4:
System.out.println("book:"+books[3]);
break;
case 5:
System.out.println("book:"+books[4]);
break;
case 6:
System.out.println("book:"+books[5]);
break;
default:
System.out.println("图书不存在,请选择其他书籍");
break;
}
}catch(InputMismatchException e){
System.out.println("输入命令错误,请输入图书序号");
}
}
}
}
}
结果显示:
热门评论
String input1 = scan.next();
switch(input1){
}
在IDE里switch后的(input1)标红线,什么原因?
public static void main(String[] args) { String[] books = {"高数","离散数学","数据结构","算法导论","数据库","JAVA语言"}; for(int i=0;;i++)
楼主,for循环数组括号里 int i=0后面还少了一个获取数组的最大长度这个条件吧