【问题标题】:Upper case letter input in scrabble game always returns 0在拼字游戏中输入大写字母总是返回 0
【发布时间】:2021-06-30 17:00:08
【问题描述】:

我为 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;
}

【问题讨论】:

  • 是的,您的 compute_score 函数对大写字母给出 0 分。仔细阅读,看看你是否能找出原因。
  • 您定义了int main(void),然后又定义了void main(void)。你不能定义main(或任何函数)两次,void main(void) 不是main 的有效原型。
  • 不要硬编码像 65 和 97 这样的值。改写 'A''a'。尽管您的代码不太可能在具有不同编码的机器上运行(它曾经更常见),但它更易于阅读。

标签: c cs50 scrabble


【解决方案1】:

考虑这段代码:

        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

无论{A} 是否被执行,第二个if 总是被执行。没有else 阻止第二个if 被执行。

根据第二个条件,{B}{C} 将被执行,因为这是带有 elseif 语句。

所以,如果有大写字母,第一个条件为真,{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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多