誰能告訴我我的代碼有什麼問題。我正在嘗試創建一個遊戲,讓電腦猜測我輸入的數字。這裏是我的代碼:電腦猜我編號C編程
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
int numberGuess = 0;
int low = 1;
int high = 100;
int computerGuess = 0;
printf("Enter a number, 1 - 100: ");
scanf("%d", &numberGuess);
while (computerGuess != numberGuess)
{
computerGuess = ((high - low) + low)/2;
printf("%d ", computerGuess);
if (numberGuess > computerGuess)
{
printf("Your guess was to low \n");
low = computerGuess+1;
}
else if (numberGuess < computerGuess)
{
printf("Your guess was to high \n");
high = computerGuess-1;
}
else if (numberGuess == computerGuess)
{
printf("Yess!! you got it!\n");
}
}
return 0;
}
你爲什麼不告訴我們什麼作品不同於你期望它開始? – 2013-03-04 19:34:19
它是功課嗎? – sschrass 2013-03-04 19:35:07
首先,你的電腦猜測每次都是一樣的,你應該讓它隨機化並在while循環開始之前聲明它。 – ryrich 2013-03-04 19:35:50