拼字游戏中输入大写字母总是 returns 0
Upper case letter input in scrabble game always returns 0
我写了一个cs50的代码。我的任务是创建一个拼字游戏。使用小写字母一切正常,但是当我尝试使用大写字母时,计算机返回给我的值始终是相同的 0。我试图自己修复它,但我只会让情况变得更糟。如果有人能告诉我怎么做,我将不胜感激。
// 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 input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
printf("Score for Player 1: %i\n", score1);
printf("Score for Player 2: %i\n", score2);
// Print the winner
if (score1 == score2)
{
printf("Tie! \n");
}
if (score1 < score2)
{
printf("Player 2 wins! \n");
}
if (score1 > score2)
{
printf("Player 1 wins! \n");
}
}
int compute_score(string word)
{
// Compute and return score for string
int compute_score = 0;
int numb;
for (int i = 0, n = strlen(word); i < n; i++)
{
if(isupper(word[i]))
{
numb = word[i] - 65;
numb = POINTS[numb];
}
if(islower(word[i]))
{
numb = word[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
compute_score += numb;
}
return compute_score;
}
考虑这段代码:
if(isupper(word[i]))
{
numb = word[i] - 65;
numb = POINTS[numb];
}
if(islower(word[i]))
{
numb = word[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
让我们用单个字母替换主体,以便我们可以专注于条件和代码流:
if(isupper(word[i]))
{A}
if(islower(word[i]))
{B}
else
{C}
看第一个if
。它的主体是{A}
,后面是另一个if
。没有 else
。当这个if
执行时,如果条件为真,就会执行{A}
。然后代码继续下if
.
第二个if
总是被执行,不管{A}
是否被执行。没有 else
阻止第二个 if
被执行。
根据第二个条件,{B}
或 {C}
将被执行,因为这是一个 if
语句和 else
.
所以,如果有大写字母,第一个条件为真,执行{A}
,第二个条件为假,所以跳过{B}
,{C}
执行。这会将 numb
设置为零,因此此例程总是为大写字母生成零,因为它同时执行 {A}
和 {C}
.
您想要的只是执行 {A}
、{B}
或 {C}
之一。为此,将第二个 if
嵌套在 else
:
中
if(isupper(word[i]))
{A}
else
if(islower(word[i]))
{B}
else
{C}
该缩进显示了这些语句的正式语法——第二个 if
在技术上位于 else
内。然而,由于这些语句很好地链接在一起,我们经常这样写:
if(isupper(word[i]))
{A}
else if(islower(word[i]))
{B}
else
{C}
现在恰好其中一个主体将被执行——{A}
如果字母是大写字母,{B}
如果字母是小写字母,{C}
否则。
我写了一个cs50的代码。我的任务是创建一个拼字游戏。使用小写字母一切正常,但是当我尝试使用大写字母时,计算机返回给我的值始终是相同的 0。我试图自己修复它,但我只会让情况变得更糟。如果有人能告诉我怎么做,我将不胜感激。
// 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 input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
printf("Score for Player 1: %i\n", score1);
printf("Score for Player 2: %i\n", score2);
// Print the winner
if (score1 == score2)
{
printf("Tie! \n");
}
if (score1 < score2)
{
printf("Player 2 wins! \n");
}
if (score1 > score2)
{
printf("Player 1 wins! \n");
}
}
int compute_score(string word)
{
// Compute and return score for string
int compute_score = 0;
int numb;
for (int i = 0, n = strlen(word); i < n; i++)
{
if(isupper(word[i]))
{
numb = word[i] - 65;
numb = POINTS[numb];
}
if(islower(word[i]))
{
numb = word[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
compute_score += numb;
}
return compute_score;
}
考虑这段代码:
if(isupper(word[i]))
{
numb = word[i] - 65;
numb = POINTS[numb];
}
if(islower(word[i]))
{
numb = word[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
让我们用单个字母替换主体,以便我们可以专注于条件和代码流:
if(isupper(word[i]))
{A}
if(islower(word[i]))
{B}
else
{C}
看第一个if
。它的主体是{A}
,后面是另一个if
。没有 else
。当这个if
执行时,如果条件为真,就会执行{A}
。然后代码继续下if
.
第二个if
总是被执行,不管{A}
是否被执行。没有 else
阻止第二个 if
被执行。
根据第二个条件,{B}
或 {C}
将被执行,因为这是一个 if
语句和 else
.
所以,如果有大写字母,第一个条件为真,执行{A}
,第二个条件为假,所以跳过{B}
,{C}
执行。这会将 numb
设置为零,因此此例程总是为大写字母生成零,因为它同时执行 {A}
和 {C}
.
您想要的只是执行 {A}
、{B}
或 {C}
之一。为此,将第二个 if
嵌套在 else
:
if(isupper(word[i]))
{A}
else
if(islower(word[i]))
{B}
else
{C}
该缩进显示了这些语句的正式语法——第二个 if
在技术上位于 else
内。然而,由于这些语句很好地链接在一起,我们经常这样写:
if(isupper(word[i]))
{A}
else if(islower(word[i]))
{B}
else
{C}
现在恰好其中一个主体将被执行——{A}
如果字母是大写字母,{B}
如果字母是小写字母,{C}
否则。