我该如何编写代码以使字母表中的每个字母都分配给拼字游戏分数?例如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"};
最佳答案
我不确定您使用的是哪种语言,但我可以为您提供一般概念。您可以使用object(javascript)或dictionairy(python)或hash(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 - 拼字游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23072184/