【发布时间】:2016-03-12 05:06:36
【问题描述】:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int random_num;
void guessGame(int guessed_num){
char answer;
if (guessed_num == random_num){
printf("Correct! That's the number.\n");
printf("Would you like to play again (y or n)?\n");
scanf("%c", &answer);
if (answer=='y')
scanf("%d", &guessed_num);
guessGame(guessed_num);
if (answer=='n')
return 0;
}
else
if (guessed_num < random_num)
printf("Too low. Guess again.\n");
else
printf("Too high. Guess again.\n");
}
int main(){
int guessed_num = 0;
srand(time(NULL));
random_num = rand() % 50 + 1;
printf("I have a number between 1-50.\n");
printf("Can you guess what it is?\n");
printf("Enter your initial guess.\n");
while(1){
scanf("%d", &guessed_num);
guessGame(guessed_num);
}
return 0;
}
所以在用户找到正确的随机数后,我希望他们能够再次玩游戏(如果他们愿意的话)。如何让代码重新运行?我尝试在我的guessGame 函数中这样做,但我知道这是错误的。
【问题讨论】: