gpt4 book ai didi

c - C语言简单猜谜游戏

转载 作者:行者123 更新时间:2023-11-30 21:45:46 25 4
gpt4 key购买 nike

你好,我正在尝试用 c 语言编写一个简单的猜数字游戏。如果玩家猜对了数字,程序会给出一条消息,询问玩家是否想再玩一次。

当我编译代码时,它会生成随机数,让玩家猜测该数字并检查该数字是否正确。但是当它询问玩家是否想再次玩时,它不会让玩家回答,而是直接退出。我不知道为什么会这样。你能帮我么?

int main()
{

int number, guess_value,number_of_guesses;
char answer;

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

A:number_of_guesses=0;
while (guess_value != number){
printf("\nEnter a number between 0 and 100\n");
scanf("%d", &guess_value);
number_of_guesses++;
if (guess_value == number){
printf("Right !!! You win\n");
}
else if (guess_value > number){
printf("\nYour guess is too high. Guess again");
}
else {printf("\nYour guess is too low. Guess again");
}
}

printf("You guessed %d times !", number_of_guesses);

printf("Do you want to play again? y? n?");
scanf("%c", &answer);
if (answer == 'y'){
goto A; /*return to to beginging*/
}
return 0;
}

最佳答案

我认为问题在于你在初始化之前比较了guess_value

关于c - C语言简单猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35460570/

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