Arrays 我是否正确嵌套数组?
我目前正在做一个练习,编写一个拼字游戏分数计算器,我试图一步一步地构建它,但我被出现的问题难住了:Arrays 我是否正确嵌套数组?,arrays,c,function,ascii,cs50,Arrays,C,Function,Ascii,Cs50,我目前正在做一个练习,编写一个拼字游戏分数计算器,我试图一步一步地构建它,但我被出现的问题难住了: #include <stdio.h> #include <cs50.h> #include <string.h> // Points assigned to each letter of the alphabet int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1,
#include <stdio.h>
#include <cs50.h>
#include <string.h>
// Points assigned to each letter of the alphabet
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};
int compute_score(string word);
int main(void)
{
// Get word from player
string word1 = get_string("Player 1: ");
// Calculate score
int score1 = compute_score(word1);
// Print score
printf("%i\n", score1);
}
int compute_score(string word)
{
// Compute and return score for string
return POINTS[word[1] - 65];
}
目前,我正试图让我的程序运行一个播放器和一个字母来测试compute_score函数,该函数将字母映射到积分数组中的分数。我对点[word[1]-65]的逻辑如下:例如,如果我输入Z,单词[1]将等于Z。ASCII中的Z是90;90 - 65 = 25; 分数[25]等于10
所以点[word[1]-65]=点[Z-65]=点[25]=10
然而,当我运行程序时,我得到的答案是0,所以我一定是把事情搞砸了,我只是错过了一些非常明显的东西吗
任何帮助都将不胜感激。word的第一个字符是word[0],因为C中的数组是基于0的。word的第一个字符是word[0],因为C中的数组是基于0的。D'oh!所以我想我们会把它放在缺失的明显的类别中。谢谢西蒙!哦!所以我想我们会把它放在缺失的明显的类别中。谢谢西蒙!在为数组编制索引之前,您可能应该对输入进行范围检查,强制使用大写,并使用字符常量“A”,而不是65:int score_index=POINTS[toupperword[0]-“A]”;然后检查得分指数