【问题标题】:For loop with IF statement C programming带有 IF 语句的 For 循环 C 编程
【发布时间】:2017-07-22 00:50:10
【问题描述】:
char userChoice;

printf("Choose how you would like to search.\nEnter A to display all players information.\
            \nEnter N to search a player based on name.\nEnter J to search a player based on jersey number.\
            \nEnter P to search a player based on position.\nEnter your choice: ");
scanf("%c", &userChoice);

do
{
    if (userChoice == 'A' || userChoice == 'a')
    {
        for (index = 0; index < count; index = index + 1)
        {
            displayPlayers(&players[index]);
        }
    }

    if (userChoice == 'J' || userChoice == 'j')
    {
        int jerseyNumber;

        printf("\nEnter jersey number for the player: ");
        scanf("%i", &jerseyNumber);

        for (index = 0; index <= MAX_PLAYERS; index++)
        {
            if (jerseyNumber == players[index].jerseyNumber)
            {
                // If the condition is met the singleDisplay function is called.
                // Containing the array of struct
                singleDisplay(&players[index]);

            }
        }
    }
    if (userChoice == 'N' || userChoice == 'n')
    {
        char playerName[LEN_NAME + 1];

        printf("\nEnter name for the player: ");
        scanf("%s", playerName);

        for (index = 0; index <= MAX_PLAYERS; index++)
        {
            if (strcmp(playerName, players[index].firstName) == 0)
            {
                singleDisplay(&players[index]);

            }
        }
    }

大部分代码仅用于上下文,我遇到的问题是无法做出 else 语句来向用户输出一条消息,即他们输入的球衣未找到。问题是 else 语句在循环内部,并且在比较数组中的所有数字时,无论多次打印它的消息。

【问题讨论】:

  • 你应该在 C 中查找 switch 语句。它在这里会很好用。
  • for(index=0; index&lt;MAX_PLAYERS; index++) { if(jerseyNumber == players[index].jerseyNumber) { singleDisplay(&amp;players[index]); break/* or set Foundflag */; } } if(index == MAX_PLAYERS){ printf("Not found!\n");
  • 那里没有“其他陈述”。再次阅读您的问题。
  • else 语句不能正常工作,所以我取出了它。
  • 不确定当 switch 语句一次只需要 1 个变量时这将如何工作。Blue idk 将如何工作 MAX_PLAYERS 被定义为 26,因为这是球衣的数量

标签: c for-loop if-statement


【解决方案1】:

您发布的代码中唯一相关的部分是:

    for (index = 0; index <= MAX_PLAYERS; index++)
    {
        if (jerseyNumber == players[index].jerseyNumber)
        {
            singleDisplay(&players[index]);
        }
    }

您问题中唯一相关的部分是:

else 语句向用户输出一条消息,表明他们输入的球衣未找到。问题是 else 语句在循环内部,并且在比较数组中的所有数字时,无论多次打印它的消息。

好的,这很清楚。您一定尝试过这样的代码(但未能向我们展示):

    for (index = 0; index <= MAX_PLAYERS; index++)
    {
        if (jerseyNumber == players[index].jerseyNumber)
        {
            singleDisplay(&players[index]);
        }
        else
        {
            printf("no match for jersey number\n");
        }
    }

当然,错误消息将被打印MAX_PLAYERS 次,或者如果找到它可能会打印MAX_PLAYERS - 1 次。所以你需要修改你的代码来明确避免这种情况:

    int found = 0;
    for (index = 0; index <= MAX_PLAYERS; index++)
    {
        if (jerseyNumber == players[index].jerseyNumber)
        {
            singleDisplay(&players[index]);
            found++;
            break;
        }
    }
    if (!found)
    {
        printf("no match for jersey number\n");
    }

break 是可选的,但它是一种很好的形式,因为它可以避免在找到匹配项后检查更多数字。也就是说,除非您想支持多个球员拥有相同的球衣号码,否则请移除 break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    相关资源
    最近更新 更多