我需要帮助完成我的程序。我似乎无法得到最后一步,当用户输入'y'
来玩另一个游戏时,它应该重新开始。如果用户输入'n'
那么它应该结束。预先感谢您的任何帮助。以下是我到目前为止的代码。C编程猜测游戏
问题出在这里:
编写一个C程序,与用户一起玩猜数字游戏。
下面是示例运行:
OK, I am thinking of a number. Try to guess it.
Your guess? 50
Too high!
Your guess? 250
Illegal guess. Your guess must be between 1 and 200.
Try again. Your guess? 30
**** CORRECT ****
Want to play again? y
Ok, I am thinking of a number. Try to guess it.
Your guess? 58
**** CORRECT ***
Want to play again? n
Goodbye, It was fun. Play again soon.
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i, number, currentGuess, MAX_GUESS = 5;
int answer = 'n';
time_t t;
srand((unsigned) time(&t));
number = rand() % 200 + 1;
printf("Welcome to the game of Guess It! \nI will choose a number between 1 and 200. \nYou will try to guess that number.");
printf("If you guess wrong, I will tell you if you guessed too high or too low. \nYou have 5 tries to get the number. \n");
printf("\nOK, I am thinking of a number. Try to guess it. \n");
for (i = 0; i < MAX_GUESS; i++) {
printf("\nYour guess?");
scanf("%i", ¤tGuess);
if (currentGuess > 200) {
printf("Illegal guess. Your guess must be between 1 and 200.\n");
printf("Try again.\n ");
}
else if (currentGuess > number) {
printf("Too high!\n");
}
else if (currentGuess < number) {
printf("Too low!\n");
}
else {
printf("****CORRECT****\n");
return 0;
}
}
printf("Sorry you have entered the maximum number of tries. \n");
printf("Want to play again? \n");
scanf("%i", &answer);
if(answer == 'n') {
printf("Goodbye, it was fun. Play again soon.\n");
return 0;
}
else if (answer != 'n') {
printf("Ok, I am thinking of a number. Try to guess it.\n");
}
}
将您的代码导出到其他方法。 int game(){....}和main()只是调用game()。然后当你想让用户重新启动时再次调用game() – Pentarex
你应该看看https://stackoverflow.com/questions/44742209/im-trying-to-code-a-number-guessing-game。看来你们都试图解决类似的编码问题。 –
然后看看'man scanf'并考虑'scanf(“%i”,&answer);'和if(answer =='n')'。具体是什么问题可能是。当你输入'n'作为答案时,'scanf'认为'n'是什么? –