我让我的程序完全工作,除了我需要程序停止询问后,5次失败的尝试。我完全不知道该怎么办。如果输入的数字连续5次不正确,那么最后会有一条抱歉的消息。我不知道如何将计数器插入到代码中,我知道我输入的可能不太正确。
备注:代码必须有多个功能。有些我宁愿自己放进一个里面,但说明是多重的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(int number){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
} else if(guess > number) {
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 0;
} else{
return 1;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %number.\n");
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 5
int guess;
int correct_answer = 1;
int wrong_answer = 1;
welcomeMessage();
number = randomNumber();
guessInput();
do {
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
guess = guess_input();
}
while(correct_answer == 1);
if(loopcount = 5){
sorryMessage
}
return 0;
}
最佳答案
我已经对您的程序做了最小的更改,以使其按预期编译和运行。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(){
int range;
srand((int)time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 1;
} else{
return 0;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %d\n",number);
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 0;
int guess;
int correct_answer;
int wrong_answer;
welcomeMessage();
number = randomNumber();
do
{
loopCount += 1;
if( loopCount > 5)
{
sorryMessage(number);
break;
}
guess = guessInput();
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
} while( !correct_answer );
return 0;
}
关于c - 5次尝试后如何结束“do…while”循环?在C中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33137993/