【发布时间】:2012-07-12 01:14:25
【问题描述】:
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);
}
}
这在线程“main”java.lang.ArrayIndexOutOfBoundsException 中不断出现异常:-1
因为它在数组字母表中找不到任何字符,请有人帮忙!
【问题讨论】:
-
出现错误时 wordArray[i] 的值是多少?还要确保您使用的都是小写字母。
-
@twain249 它提供了单词中的第一个字母。所以如果我在“测验”中写下它会打印“q”。并且 index 以 -1 打印出来。
标签: java arrays exception compiler-errors