【问题标题】:How to Fix the Logic Errors in My "Guess the Movie" Game as a C Program如何将我的“猜电影”游戏中的逻辑错误修复为 C 程序
【发布时间】:2019-10-07 17:17:33
【问题描述】:

我用 C 编程语言编写了一个 Guess the Movie 游戏。我的逻辑似乎是正确的,但是每当我运行程序时,它都无法按预期工作。

这是我的代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ran = 1;
    int l, j = 0, i = 0, total = 0, d = 0;
    char b;
    char a[20];
    char s[1000];
    int z;
    FILE *my;

    printf("Enter your name:\n ");
    scanf("%s", s);

    ran = rand() % 6;
    if (ran == 1)
    {
        my = fopen("my1.txt", "r");
    }
    else if (ran == 2)
    {
        my = fopen("my.txt", "r");
    }
    else if (ran == 3)
    {
        my = fopen("my2.txt", "r");
    }
    else if (ran == 4)
    {
        my = fopen("my3.txt", "r");
    }
    else if (ran == 5)
    {
        my = fopen("my4.txt", "r");
    }

    for (d = 0; d < 20; d++)
        fscanf(my, "%c", &a[d]);

    fclose(my);

    printf("GUESS THE MOVIE GAME\n");

    for (j = 0; j < 7; j++)
    {
        if (a[j] == 'm')
        {
            printf("M ");
        }
        else
        {
            printf("_ ");
        }
    }

    printf("\n");
    printf("Let's begin the game\n");
    for (i = 0; i < 7;)
    {             
        if (a[i] != 'm')
        {
            printf("enter character number %d\n",i+1);
            scanf("%c", &b);
            if (b == a[i])
            {
                printf("its a right guess\n");
                total = total + 4;
                i++;
            }
            else if (b != a[i])
            {
                printf("Wrong choice\n");
                if (total == 1 || total == 0)
                {
                    total=0;
                }
                else
                {
                    total = total - 2;
                }
            }
        }
    }
    printf("You have guessd the movie\n");
    printf("The movie name is: ");
    for (i = 0; i < 7; i++)
    {
        printf("%c",a[i]);
    }
    printf("Your score is %d\n",total);
}

这是我每次运行上述代码时得到的程序输出:

输入您的姓名:
拉吉
猜电影游戏
_ _ _ _ M _ _
让我们开始游戏
输入字符编号 1
错误的选择
输入字符编号 1

错误的选择
输入字符编号 1
错误的选择
输入字符编号 1

【问题讨论】:

  • 这个波浪形缩进是有意的吗?抱歉,我们无法读取这样的代码。
  • 尝试打印在生成时用完。
  • 注意:ran=rand()%6;6 不同的值,但您只决定打开 5 文件。当ran == 0 时会发生什么?此外,您每次都将使用相同的问题列表,直到您在 main() 的顶部添加 srand((unsigned)time(NULL))。并且请将出现的"%c" 更改为" %c" 以过滤留在输入缓冲区中的换行符。
  • 重新措辞问题文本。格式化 C 代码。
  • OT: about: scanf("%s", s); 1) 始终检查返回值以确保操作成功。在这种情况下:if( scanf("%s", s) != 1 ) { // handle error } 2) the input format specifier: %s`(和[...])总是将 NUL 字节附加到输入。为避免可能的缓冲区溢出和导致未定义的行为,请始终使用比输入缓冲区长度小 1 的 MAX CHARACTER 修饰符。在这种情况下:scanf("%999s", s);

标签: c logic


【解决方案1】:

除了在cmets中指出的不足之外,还有一个主要的逻辑错误:

    for (i = 0; i < 7;)
    {             
        if (a[i] != 'm')
        {
            …
        }
    }

如果循环遇到m,它将无限重复。删除if (a[i] != 'm') 或添加else ++i

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多