gpt4 book ai didi

c - 我正在尝试编写一个数字猜谜游戏

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:57 26 4
gpt4 key购买 nike

我到这里了,但我仍然需要以某种方式使用 while 循环。 "want to play again(y/n)"and "Illegal guessing. 你的猜测必须在 1 到 200 之间。再试一次。你的猜测?"似乎不起作用。请帮助我处理 while/do-while 循环并解决我上面的两个问题。谢谢。

#include <stdio.h>

int main()
{
int i,number,guess,tries=5,answer;

printf("Welcome to the game of Guess It!\nI will choose a number between 1 and 200.");
printf("\nYou will try to guess that number.If you guess wrong, I will tell you if you guessed too high or too low.");
printf("\nYou have 5 tries to get the number.\n\nOK, I am thinking of a number. Try to guess it.");

srand(time(NULL));
number = rand() % 200 + 1;

for (i=0;i<tries;i++) {
printf("\n\nYour guess? ");
scanf("%i",&guess);

if (guess==number) {
printf("**** CORRECT ****\n\nWant to play again(y/n) ");
scanf("%i",&answer);

if (answer=='y') {
return (i=0);
}
else (answer=='n'); {
printf("Goodbye, It was fun. Play again soon.");
}

}
else if (guess>number) {
printf("Too high!");
}
else if (guess<number) {
printf("Too low!");
}
else (guess>200); {
printf("Illegal guess. Your guess must be between 1 and 200.\nTry again. Your guess?");
}
}

printf("\n\nSorry, you ran out of tries.\n\nWant to play again?(y/n) ");
scanf("%i",&answer);

if (answer=='y') {
return (i=0);
}
else if (answer=='n'); {
printf("Goodbye, It was fun. Play again soon.");
}

return 0;
}

最佳答案

首先,也是最重要的,打开警告。您的代码中有几个基本错误会被编译器警告捕获。不幸的是,默认情况下它们是关闭的。 -Wall 打开基本警告。这不是“所有”警告,因为这是 C! -fsanitize=address -Wall -Wshadow -Wwrite-strings -Wextra -Wconversion -std=c99 -pedantic 是一组很好的警告。


您可以在循环周围放置一个循环,但这很快就会变得难以维护。相反,将游戏放入一个函数中并围绕它循环。

void do_game(int tries) {
int number = rand() % 200 + 1;

for (int i=0; i < tries; i++) {
int guess;
printf("\n\nYour guess? ");
scanf("%i",&guess);

if (guess == number) {
printf("**** CORRECT ****\n\n");
return;
}
else if (guess > number) {
printf("Too high!");
}
else if (guess < number) {
printf("Too low!");
}
else if (guess > 200) {
printf("Illegal guess. Your guess must be between 1 and 200.\nTry again. Your guess?");
}
}

puts("\n\nSorry, you ran out of tries.\n\n");
return;
}

请注意游戏只需要关注游戏本身。没有关于玩其他游戏的其他逻辑或问题。并且可以在游戏结束后立即返回。

然后程序的其余部分就很简单了。无限循环运行游戏,完成后退出游戏。

int main() {
printf("Welcome to the game of Guess It!\nI will choose a number between 1 and 200.");
printf("\nYou will try to guess that number.If you guess wrong, I will tell you if you guessed too high or too low.");
printf("\nYou have 5 tries to get the number.\n\nOK, I am thinking of a number. Try to guess it.");

srand(time(NULL));

while(1) {
do_game(5);

char answer;
printf("Want to play again?(y/n) ");
scanf("%c",&answer);

if (answer == 'n') {
printf("Goodbye, It was fun. Play again soon.");
break;
}
}

return 0;
}

有问题,是scanf。它总是 scanfscanf就是这样的问题,there's a whole FAQ for it .

scanf("%i") 读取单个整数但不读取后面的换行符。该换行符和任何其他额外输入都在标准输入上徘徊。稍后的 scanf("%c", &answer); 可能会读取该换行符而不是他们的答案。

scanf("%i\n") 没有解决问题。这告诉 scanf 读取一个整数,然后是一个换行符,然后寻找另一个非空白字符。 scanf 很奇怪。

使用 fgets 读取整行并使用 sscanf 解析它会更好。你可以为进入 variadic arguments 的那个写一个小实用函数.

void line_scanf( const char *fmt, ... ) {
// Get the list of arguments.
va_list args;
va_start(args, fmt);

// Read a line.
char line[256];
fgets(line, sizeof(line), stdin);

// Scan the line like sscanf() but with a va_list.
vsscanf( line, fmt, args );

// Close the list of arguments.
va_end(args);
}

然后像scanf一样使用它。它保证读取整行并且不会在缓冲区中留下换行符或部分输入。

    int guess;
printf("\n\nYour guess? ");
line_scanf("%i",&guess);

关于c - 我正在尝试编写一个数字猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com