您好我正在尝试在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;
}
答案 0 :(得分:4)
我认为问题在于您在初始化之前比较guess_value
。
答案 1 :(得分:1)
更正后的代码是:
int main()
{
int number, guess_value,number_of_guesses;
char answer;
srand((unsigned)time(NULL));
do
{
number=rand() %100+1;
number_of_guesses=0;
do
{
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");
}
}
while (guess_value != number);
printf("You guessed %d times !\n", number_of_guesses);
printf("Do you want to play again? y? n?");
scanf(" %c", &answer);
}
while (answer == 'y');
return 0;
}
你的代码中有很多问题:
guess_value
,或将循环更改为do{}while(guess_value != number)
scanf("%d", &guess_value);
while (answer == 'y');
循环的init。否则它总是使用相同的数字来猜测。要更具体地说明第3点,您必须了解第一个scanf
不使用用户输入的'\n'
(回车键,输入键盘键)字符。当scanf
返回到answer变量时,此char仍然在输入缓冲区中,直到执行第二个'\n'
。最后,当您检查if (answer == 'y')
时,条件始终为假。
答案 2 :(得分:0)
避免使用goto语句。使用do-while而不是
do {
number=rand() %100+1;
number_of_guesses=0;
do {
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");
}
} while (guess_value != number);
printf("You guessed %d times !", number_of_guesses);
printf("Do you want to play again? y? n?");
scanf("%c", &answer);
} while (answer == 'y');
这应该可以解决您的问题,它会在代码中循环,直到出现“y”之外的任何内容。
答案 3 :(得分:0)
你的问题是你应该重置guess_value,而不会被执行
答案 4 :(得分:0)
#include<stdio.h>
#include<stdlib.h> //for rand & srand function
#include<time.h> //for time obviously.
int main()
{
int number, guess_value,number_of_guesses;
char answer;
A: //put label here, Generate rand val every time.
srand((unsigned)time(NULL));
number=rand() %100+1;
guess_value=0; //reset the guess value.
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?");
getchar(); //use this to remove the \n.
scanf("%c", &answer);
if (answer == 'y'){
goto A; /*return to to beginging*/
}
return 0;
}
scanf从输入缓冲区获取输入,
每次用户点击输入时,\n
都会进入缓冲区。
执行此scanf功能后,请执行\n
。
答案 5 :(得分:-1)
这有效:
#include <stdio.h> // Include
int main() {
int number, guess_value, number_of_guesses;
char answer;
A:
srand(time(NULL)); // Fix
number = rand() %100+1;
number_of_guesses = 0;
while(guess_value != number) {
puts("\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 !\n", number_of_guesses);
puts("Do you want to play again? y? n?");
scanf("%s", &answer); // Fix
if (answer == 'y'){
goto A;
}
return 0;
}