我一直在這個項目上花了幾個小時試圖弄清楚這一點,但我到了腦死亡的地步,我讀的所有東西都讓我感到困惑。猜數字遊戲,每#低
這個想法是輸入一個數字,程序會告訴我它是對還是錯。每次輸入數字後的最終響應都是數字太低。
另外,最終答案指出答案太低,而且它在同一時間是正確的。
最後,這件事情是假設再次問是否輸入的數字是不正確的,但我不知道如何做到這一點。
字面上,最小的建議是非常讚賞在這一點上。這是一個漫長而呻吟的夜晚。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
//new function
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
//new function
int randomNumber(){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return 0;
}
//new function
int guessInput(){
int guess, range;
printf("I'm thinking of a number between 1 and 20\n");
printf("Care to give it a guess? Be careful! You only get 4 tries!\n");
scanf("%d", &guess);
return 0;
}
//new function
int wrongAnswer(){
int guess, number;
if(guess < number)
{
printf("Try again, your guess is too low\n");
return 0;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit to high\n");
return 0;
}
return 0;
}
//new function
int correctAnswer(){
int guess, number;
if(guess == number)
printf("Great job! That time you got it right!\n");
return 0;
}
int main(){
welcomeMessage();
randomNumber();
guessInput();
wrongAnswer();
correctAnswer();
}
您正在爲每個函數定義新變量 - 這些變量不是「全局」的,只存在於這些函數中。 – teppic
每次打電話給'rand'時都不要調用'srand'。在程序啓動時調用一次。 –
@JonathonReinhart對不起,你能解釋一下嗎?還是可能給我看?我不太明白。 –