我是否正确嵌套数组?
Am I correctly nesting arrays?
我目前正在做一个练习来编写一个拼字游戏分数计算器,我试图一次一步地构建它,但我对出了什么问题感到困惑:
#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
将字母映射到POINTS
数组中的分数的功能。我对 POINTS[word[1] - 65]
的逻辑如下:如果我输入 Z
,例如,word[1]
将等于 Z
。 ASCII 中的“Z”为 90; 90 - 65 = 25; POINTS[25]
是 10
.
所以POINTS[word[1] - 65]
= POINTS[Z - 65]
= POINTS[25]
= 10
.
然而,当我 运行 程序时,我收到了 0
的答案,所以我一定是搞砸了,我是不是漏掉了一些非常明显的东西?
如有任何帮助,我们将不胜感激。
word的第一个字符是word[0]
,因为C中的数组是从0开始的。
我目前正在做一个练习来编写一个拼字游戏分数计算器,我试图一次一步地构建它,但我对出了什么问题感到困惑:
#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
将字母映射到POINTS
数组中的分数的功能。我对 POINTS[word[1] - 65]
的逻辑如下:如果我输入 Z
,例如,word[1]
将等于 Z
。 ASCII 中的“Z”为 90; 90 - 65 = 25; POINTS[25]
是 10
.
所以POINTS[word[1] - 65]
= POINTS[Z - 65]
= POINTS[25]
= 10
.
然而,当我 运行 程序时,我收到了 0
的答案,所以我一定是搞砸了,我是不是漏掉了一些非常明显的东西?
如有任何帮助,我们将不胜感激。
word的第一个字符是word[0]
,因为C中的数组是从0开始的。