[英]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];
}
目前,我只是想让我的程序与一个玩家和一个字母一起运行,以测试将字母映射到POINTS
数组中的分数的compute_score
function。 我对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 中的 arrays 是从 0 开始的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.