怎么回事呢?
package borrowBook;
import java.util.Scanner;
public class BorrowBook {
? ?public static void main(String[] args) {
? ? ? ?String[] bookList =new String[4];
? ? ? ?bookList[0]="高數";
? ? ? ?bookList[1]="數據結構";
? ? ? ?bookList[2]="大學英語";
? ? ? ?bookList[3]="軍事理論";
? ? ? ?System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
? ? ? ?int order=0;
? ? ? ?Scanner scanner=new Scanner(System.in);
? ? ? ?try{
? ? ? ? ? ?order=scanner.nextInt();
? ? ? ?}catch (Exception e){
? ? ? ? ? ?System.out.println("命令輸入錯誤!請根據提示輸入數字命令");
? ? ? ?}
? ? ? ?switch (order){
? ? ? ? ? ?case 1:
? ? ? ? ? ? ? ?System.out.println("輸入圖書名稱:");
? ? ? ? ? ? ? ?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("圖書不存在!");
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case 2:
? ? ? ? ? ? ? ?System.out.println("輸入圖書序號:");
? ? ? ? ? ? ? ?int num=0;
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?num=scanner.nextInt();
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?System.out.println("命令輸入錯誤!請根據提示輸入數字命令");
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?System.out.println("book:"+bookList.get(num-1));
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?System.out.println("圖書不存在!");
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?default:
? ? ? ? ? ? ? ?System.out.println("抱歉,沒有此項命令!");
? ? ? ?}
? ?}
}
2023-05-27
你這個應該是數組越界,for循環里i<bookList.length,把“=”去掉看一下。