java 中的拼字游戏数组

Scrabble array in java

所以对于 class,我需要制作一个程序,根据字母的拼字游戏值对单词进行评分。所以 aa 应该是 2 因为 1 a 值 1 分。但是由于某些原因,我的程序只显示 aa 为 1 点。

import java.util.Scanner;

public class Scrabble {   
       public static void main(String[] args) {
            String[] 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[] values = {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};

            Scanner kboard = new Scanner(System.in);
            System.out.println("Input your word.");

            String choice = kboard.nextLine();

            int score = 0;
            for(int i = 0; i < choice.length(); i++) {
                for(int n = 0; n < 26; n++)   {
                    if(choice.substring(i).equals(alphabet[n]))
                    {
                       score += values[n];
                    }
                }
             }

             System.out.println("The score for your word is " + score);
       }

}

因为 "aa".substring(0) == "aa" 其中 != "a"。所以基本上这是一个昂贵的 'score last char'.

改用choice.charAt(索引)。 (另外,您可以只使用一个字符来映射查找以跳过对每个字符的迭代)

您使用的子字符串版本错误,我假设您想使用 substring(int beginIndex, int endIndex)

这个版本的子串应该能让你 select 只有 一个 字符子串来与你的字母数组进行比较。但是,当使用 substring(int beginIndex) 时,子字符串以指定索引处的字符开始并延伸到该字符串的末尾,这意味着您 selecting 多于 one 字符子串(除非你在 alphabet.length-1 )。

或者要解决手头的问题,只需将您的 if 条件更改为:

if(Character.toString(choice.charAt(i)).equals(alphabet[n]))

问题是 someStr.substring(i) 不 return 只有一个字母。比如System.out.println("aaa".substring(1));的输出是"aa"不是a.

这是修改后的版本:

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[] values = {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};
Scanner kboard = new Scanner(System.in); System.out.println("Input your word.");
String choice = kboard.nextLine();
int score = 0;
for(int i = 0; i < choice.length(); i++) {
  for(int n = 0; n < 26; n++) {
    // Use charAt here instead of substring
    if(choice.charAt(i) == alphabet[n])
    {
      score += values[n];
    }
  }
}
System.out.println("The score for your word is " + score);

}

更好的是,您可以只对字母表使用哈希 Table 并避免内部循环。

试试这个解决方案:

import java.util.Scanner;

public class Scrabble {
    public static void main(String[] args) {
        String[] 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[] values = {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};

        Scanner kboard = new Scanner(System.in);
        System.out.println("Input your word.");
        String choice = kboard.nextLine();

        int score = 0;
        for(int i = 0; i < choice.length(); i++) {
            for(int n = 0; n < 26; n++) {
                if(choice.substring(i, i + 1).equals(alphabet[n])) {
                    score += values[n];
                }
            }
        }

        System.out.println("The score for your word is " + score);
    }
}

基本上,您没有正确使用 substring() 。您只输入了一个开始索引,这意味着它从该点开始读取,直到字符串结束。我将其更改为读取结束索引,在您的情况下为 i+1

基本上,在你的第一次迭代中,你在 alphabet 中寻找 "aa",而只有在你的第二次迭代中,你才在 alphabet 中寻找 "a",这是为什么您在第一次迭代中没有获得第一个“1 分”。