Java-ArrayOutOfBoundsException帮助我 import java.util.*; 导入java.util.array; 公开课计分卡{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 字符[]字母表={'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[]分数={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,1,4,4,8,4}; System.out.println(“输入单词:”); String word=in.nextLine(); 整数总分=0; char[]wordArray=word.toCharArray(); for(int i=0;i

Java-ArrayOutOfBoundsException帮助我 import java.util.*; 导入java.util.array; 公开课计分卡{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 字符[]字母表={'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[]分数={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,1,4,4,8,4}; System.out.println(“输入单词:”); String word=in.nextLine(); 整数总分=0; char[]wordArray=word.toCharArray(); for(int i=0;i,java,arrays,exception,compiler-errors,Java,Arrays,Exception,Compiler Errors,indexOf(wordArray[i])返回-1。我怀疑这是由于大写字母和/或特殊字符造成的。请先执行此操作并添加错误检查: import java.util.*; import java.util.Arrays; public class ScoreCalc { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] alphabet

indexOf(wordArray[i])
返回-1。我怀疑这是由于大写字母和/或特殊字符造成的。请先执行此操作并添加错误检查:

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);
    }
}
不管怎样,我会做这样的事情,因为它更干净:

word.toLowerCase().toCharArray()
然后

String alphabet = "abcdefghijklmnopqrstuvwxyz";

因此,我要做的第一件事是使其完全面向对象,如下所示:

int index = alphabet.indexOf(wordArray[i]);
if(index == -1) {
    // handle the special character
} else {
    totalScore += score[index];
}
然后在主程序中,您将执行以下操作:

public class CharacterScore  //name this whatever makes you happy
{  
   int value;  
   char character;  

   public CharacterScore(int value, char character)  
   {  
     this.value=value;  
     this.character=character;  
   }  //getters/setters  
}  

这不一定是最好的方法,但我想帮助您理解这些概念。

问题的原因是方法
数组的参数。asList
是泛型vararg
(T…a)
,您使用的是基元字符数组


解决方案:使用对象
Character[]alphabet={'a','b',…}
而不是原语
char[]alphabet={'a','b',…}
,因为
T…
不是将
char[]alphabet
作为对象数组,而是作为一个对象,因此您的列表将只包含对该数组的引用。

wordArray[i]的值是多少当你得到错误时?还要确保你使用的是所有小写字母。@twain249会出现单词中的第一个字母。因此,如果我在“测验”中写下,它会打印“q”。索引会打印出-1。
private static List<CharacterScore> characterScores;  
static  
{  
   characterScores = new ArrayList<CharacterScore>();  
   String alphabet = "abcdefghijklmnopqrstuvwxyz";  
   for(char current : alphabet.toCharArray())  
   {  
     characterScores.add(new CharacterScore((int)Math.random() *10), current));
   }  
} 
for(CharacterScore current : characterScores)  
{  
    for(int i = 0; i <wordArray.length; i++)  
    {  
       if(current.getCharacter() == wordArray[i])  
       {  
             recordScore(current.getValue());  
       }  
    }  
}