【问题标题】:Number guessing game in CC语言猜数字游戏
【发布时间】:2018-09-13 14:51:32
【问题描述】:

我在用 C 语言进行数字猜谜游戏时遇到问题。在运行程序时,当我将 char 元素放入其中时,它会显示返回函数。有人可以帮我弄这个吗?另外,如何改进我的代码以使其可行?我真的被这个问题困住了。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main()
{
    char s, n, q;
    int b;
    bool (1=true), (0=false);
    int secret;
    secret = rand();
    int guess;
    int seed;
    srand(seed);
    printf("Welcome to the guessing game!\n");
    do{
        printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
        scanf("%s, %s, %s", s, n, q);
        if((s==1))
        {
            printf("The secret number is Between 0 AND rand(). Guess\n");
            scanf("%s", b);
        }
        else if((n ==1))
        {
            printf("Enter a new MAXIMUM\n");
            scanf("%s", rand());
            if(( s ==1))
            {
                printf("The secret number is Between 0 AND rand(). Guess\n");
                scanf("%s", b);

                printf("The secret number is between 0 and rand() Guess:");
                scanf("%s", b);
                if(guess = rand()){
                    printf("Congratulations you won, You took %d guesses!", b);
                    break;
                }
                else if(guess > rand())
                {
                    printf("Too High, Guess again:");
                }
                else if(guess < rand()){
                    printf("Too Low, Guess Again:");
                }
                else{
                    printf("This number out of the number set!");
                }
            }
        }
        else{
            printf("Unrecognized command");
        }
    }while(q == 1);
    printf("You quited the game");

    return 0;
}

【问题讨论】:

  • 您的scanf 有很多错误。例如 scanf("%s", b); 应该是 scanf("%d", &amp;b); 我建议你先阅读有关 scanf 文档。
  • 这将scanf("%s", rand()); 导致UB。
  • 你用的是什么编译器?
  • 您希望用户在您的菜单中输入什么选项?每个选项一个字符还是一个是/否字符串? scanf("%s, %s, %s", s, n, q); 您应该阅读scanf 的联机帮助页并再次考虑格式字符串。还有关于参数。
  • 重要提示:第​​一件事!您需要掌握scanf 的用法,然后才能在更大的上下文中使用它。只有在您能够获得用户输入后,您才能尝试处理不同的菜单选项。要验证您的输入是否正确,您可能需要阅读How to debug small programs

标签: c


【解决方案1】:

此代码有无数问题需要解决。我建议您分小步进行代码编写过程。看起来好像您一口气编写了整个程序,运行它,然后发现它不起作用,而不是逐渐添加小功能并运行每个功能以验证它是否可以工作,然后再继续下一步。不这样做会导致程序难以调试,并且对程序的运行方式缺乏了解。

具体来说,尝试编写一个三行或四行程序来收集和打印用户输入。输出是否按预期工作?您是否测试了它在各种输入上的稳健性?你能把它写成使用多种数据类型吗?如果出现问题,您是否在继续前行之前研究并解决了问题?

需要调查的程序的一些领域:

  • bool (1=true), (0=false); 不会编译,也不是程序所必需的。如果你#include &lt;stdbool.h&gt;你不需要这样做(你可以简单地写if (something == true))。
  • srand() 未正确调用或播种。使用您包含的标题中的time() 调用为每个程序播种一次,并在每场游戏中调用一次rand()。使用% 运算符将函数输出设置在0max 之间。
  • 在每个回合中,将 guess 与您之前存储的值 rand() 进行比较,而不是在每次比较期间调用 rand(),这使得游戏逻辑任意。
  • %s 输入不合适;读取chars %cints %d,将适当的变量引用传递给scanfscanf("%s, %s, %s", s, n, q); 收集 3 个空格分隔的字符串作为输入,而不是提示建议的 1 个 char
  • 没有游戏循环。当用户选择s 并将您的猜测/响应逻辑移到那里时,添加一个内部循环来运行游戏。
  • 使用更详细的变量名称并更正括号和缩进以提高可读性。

总而言之,这是一个可能的工作版本。它可以更好地利用函数并实现安全的用户输入(读者练习):

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

int main() {
    char menu_choice;
    int guess;
    int guesses;
    int secret;
    int max = 100;
    srand(time(NULL));
    printf("Welcome to the guessing game!\n");
    
    for (;;) {
        printf("\nMenu: (s) to start a new game, (n) to set a new range, or (q) to quit: ");
        scanf(" %c", &menu_choice);

        if (menu_choice == 's') {
            guesses = 0;
            secret = rand() % max;
            
            for (;;) {
                printf("\nThe secret number is between 0 and %d. Enter a guess: ", max);
                scanf("%d", &guess);
                guesses++;

                if (guess == secret) {
                    printf("\nCongratulations, you won! You guessed %d in %d guesses!\n", secret, guesses);
                    break;
                }
                else if (guess > secret) {
                    printf("Too high! Guess again.");
                }
                else if (guess < secret) {
                    printf("Too low! Guess again.");
                }
                else if (guess >= max) {
                    puts("Out of range");
                }
            }
        }
        else if (menu_choice == 'n') {
            printf("\nEnter a new maximum: ");
            scanf("%d", &max);
        }
        else if (menu_choice == 'q') {
            puts("\nGoodbye!");
            break;
        }
        else {
            puts("\nUnrecognized command.");
        }
    }

    return 0;
}

示例运行:

Welcome to the guessing game!

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  n

Enter a new maximum:  50

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  s

The secret number is between 0 and 50. Enter a guess:  25
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  37
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  43
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess:  47

Congratulations, you won! You guessed 47 in 4 guesses!

Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:  q

Goodbye!

【讨论】:

  • 哇,谢谢!我知道我的代码完全是看起来很垃圾的程序(如果这有意义的话),很抱歉,这是因为我是这个领域的新手,我想弄清楚程序是如何工作的以及其他东西,我试着做我最好)))
  • 没问题,我们都从某个地方开始了。慢慢来,逐步建立。
【解决方案2】:
  printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
  scanf("%s, %s, %s", s, n, q);

您不需要使用三个变量 s、n、q。您应该要求用户输入一个选项。要么开始新游戏,要么退出或其他任何事情。

其次,rand() 每次都会返回一个随机数。你应该在某处存储随机数。像这样

rand_num=rand()

还有

if(guess = rand())

是一种错误的比较方式。这应该是

if(guess==rand_num)

最后,二分搜索是您问题的解决方案。请在网上参考

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2016-06-21
    • 2013-10-09
    相关资源
    最近更新 更多