拼字游戏 cs50 计算分数
Scrabble cs50 computescore
部分预设回复未被正确检查。
请不要给出答案,只是希望得到一些改进的指导。
预设回复:“嗨!”应该战胜“哦”,“计算机”应该战胜“科学”。
下面是我的 computescore 函数代码;
int compute_score(string word)
{
int j = strlen(word);
int total = 0;
int index;
for (int i = 0; i < j; i++)
{
char c = word[i];
if (isupper(c))
{
c = c - 65;
index = c;
total = POINTS[index];
}
if (islower(c))
{
c = c - 97;
index = c;
total = POINTS[index];
}
}
推测您正在尝试对角色得分值进行求和。
此行将 total
变量重新分配给每次迭代的最新奇点值:
total = POINTS[index];
如果您在 for
循环之后查看 total
的值,您会发现它是字符串中最后一个有效字符的值。
相反,请使用 plus-equals 运算符随时添加:
total += POINTS[index];
并且不要忘记 return 来自该函数的内容。
部分预设回复未被正确检查。
请不要给出答案,只是希望得到一些改进的指导。
预设回复:“嗨!”应该战胜“哦”,“计算机”应该战胜“科学”。
下面是我的 computescore 函数代码;
int compute_score(string word)
{
int j = strlen(word);
int total = 0;
int index;
for (int i = 0; i < j; i++)
{
char c = word[i];
if (isupper(c))
{
c = c - 65;
index = c;
total = POINTS[index];
}
if (islower(c))
{
c = c - 97;
index = c;
total = POINTS[index];
}
}
推测您正在尝试对角色得分值进行求和。
此行将 total
变量重新分配给每次迭代的最新奇点值:
total = POINTS[index];
如果您在 for
循环之后查看 total
的值,您会发现它是字符串中最后一个有效字符的值。
相反,请使用 plus-equals 运算符随时添加:
total += POINTS[index];
并且不要忘记 return 来自该函数的内容。