import java.util.*;
import java.util.Arrays;
public class ScoreCalc {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int[] score = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
System.out.println("Enter word: ");
String word = in.nextLine();
int totalScore = 0;
char[] wordArray = word.toCharArray();
for(int i=0; i<wordArray.length; i++) {
System.out.println(wordArray[i]);
int index = Arrays.asList(alphabet).indexOf(wordArray[i]);
System.out.println(index);
totalScore = totalScore + score[index];
}
System.out.println(totalScore);
}
}
這使得螺紋想出異常「主」 java.lang.ArrayIndexOutOfBoundsException:-1爪哇 - ArrayOutOfBoundsException幫我
,因爲它無法找到任何的字符數組中字母的人可以幫助PLZ!
當你得到錯誤時,wordArray [i]的值是多少?另外請確保您使用的是全部小寫字母。 – twain249 2012-07-12 01:16:29
@ twain249它提出了單詞中的第一個字母。所以如果我在「測驗」中寫道,它會打印「q」。並用-1打印索引。 – user1516514 2012-07-12 01:18:53