【问题标题】:Scrabble Sort Of拼字游戏
【发布时间】:2014-04-14 23:32:44
【问题描述】:

如何制作一个代码,以便将字母表中的每个字母分配给一个拼字游戏分数?例如 a= 1 分,b= 3 分,c= 3 分。然后,如果我有字符串“abc”,则字符串的值将是 7 点。到目前为止我的想法是这两个数组:

private int[] Points= {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};
private String Letters[] = {"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"};

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    我不确定您使用的是什么语言,但我可以帮助您了解一般概念。您可以使用对象(javascript)或字典(python)或哈希(ruby)并为每个字母分配一个分值,如下所示

    scores = {
        "a":1,
        "b":3,
        "c":3
    }
    

    以此类推,直到 z 然后要找到分数,您可以使用 javascript 示例执行类似的操作,但通过使用循环在其他语言中非常相似

    var word = abc;
    var score = 0;
    for(i=0;i<word.length;i++){
        result += scores[i]
    }
    

    【讨论】:

    • 语言 (Java) 在问题的标签列表中。
    • 分数是什么类型的对象?它是 int 数组还是 string 数组?
    • scores 只是一个由键组成的普通对象:值对它不是 int 字符串也不是数组我不是 100% 确定它的名称,但我认为它被称为 javascript 中的对象,字典在 python 和 ruby​​ 中的哈希和 php 中的关联数组。
    【解决方案2】:

    您可以使用HashMapget() 部分将返回 3。String 也可以使用 Character 更改为字符。

    HashMap<String, Integer> map=new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 3);
    
        map.get("b");
    

    【讨论】:

      【解决方案3】:

      我会使用一个哈希图,其中键是 char(字母),值是 int(分数)。

      【讨论】:

        【解决方案4】:

        你可以有一个Map:

        private static final Map<Character, Integer> letterValues = new HashMap<>();
        
        static {
            letterValues.put('a', 1);
            letterValues.put('b', 3);
            // ...
            letterValues.put('z', 10);
        }
        

        然后做一个类似的方法:

        public int getWordValue(String word) {
            int value = 0;
            for(int i = 0; i < word.length(); i++) {
                value += letterValue.get(word.charAt(i));
            }
            return value;
        }
        

        或者,您可以使用开关来做到这一点:

        public int getLetterValue(char letter) {
            switch(letter) {
            case 'a': return 1;
            case 'b': return 3;
            // ...
            case 'z': return 10;
            default: return 0;
            }
        }
        

        【讨论】:

          【解决方案5】:
          public static void display() {
              int[] points = { 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 };
              String letters[] = { "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" };
          
              //Holding alphabet's corresponding value in a map
              Map<String, Integer> tempMap = new HashMap<String, Integer>();
              for (int i = 0; i < letters.length; i++) {
                  tempMap.put(letters[i], points[i]);
              }
          
              //Test Data
              String test = "abc";
              int sum = 0;
              for (int i = 0; i < test.length(); i++) {
                  sum += tempMap.get(test.charAt(i)+"");//making character to string
              }
              System.out.println(sum);
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-02
            • 1970-01-01
            • 1970-01-01
            • 2010-12-23
            • 1970-01-01
            相关资源
            最近更新 更多