怎么回事呢?
package borrowBook;
import java.util.Scanner;
public class BorrowBook {
? ?public static void main(String[] args) {
? ? ? ?String[] bookList =new String[4];
? ? ? ?bookList[0]="高數(shù)";
? ? ? ?bookList[1]="數(shù)據(jù)結(jié)構(gòu)";
? ? ? ?bookList[2]="大學(xué)英語(yǔ)";
? ? ? ?bookList[3]="軍事理論";
? ? ? ?System.out.println("輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
? ? ? ?int order=0;
? ? ? ?Scanner scanner=new Scanner(System.in);
? ? ? ?try{
? ? ? ? ? ?order=scanner.nextInt();
? ? ? ?}catch (Exception e){
? ? ? ? ? ?System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令");
? ? ? ?}
? ? ? ?switch (order){
? ? ? ? ? ?case 1:
? ? ? ? ? ? ? ?System.out.println("輸入圖書(shū)名稱:");
? ? ? ? ? ? ? ?String bookName;
? ? ? ? ? ? ? ?bookName=scanner.next();
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?for (int i=0;i<= bookList.length;i++);
? ? ? ? ? ? ? ? ? ?String book=bookList.get(i);
? ? ? ? ? ? ? ? ? ?if(bookName.equals(book)){
? ? ? ? ? ? ? ? ? ? ? ?System.out.println("book:"+book);
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?System.out.println("圖書(shū)不存在!");
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 2:
? ? ? ? ? ? ? ?System.out.println("輸入圖書(shū)序號(hào):");
? ? ? ? ? ? ? ?int num=0;
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?num=scanner.nextInt();
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令");
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?System.out.println("book:"+bookList.get(num-1));
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?System.out.println("圖書(shū)不存在!");
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?default:
? ? ? ? ? ? ? ?System.out.println("抱歉,沒(méi)有此項(xiàng)命令!");
? ? ? ?}
? ?}
}
2023-05-27
你這個(gè)應(yīng)該是數(shù)組越界,for循環(huán)里i<bookList.length,把“=”去掉看一下。