好吧,這個程序要求用戶輸入一個下限和上限,對象是程序根據反饋系統(-1,0,1)猜測數字。一切正常,每次我給它反饋時都會編譯它,它會將數字加倍,例如查看輸出。任何幫助將大大可以理解的,因爲由於C猜測遊戲上下界
我還在學習總是開放的建議#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
int low;
int high;
int Guess;
int feedback;
printf("Enter Upper bound number: ");
scanf("%d", &high);
printf("Enter lower bound number: ");
scanf("%d", &low);
Guess = (high - low)/2+low;
while (feedback != 0)
{
printf("my guess is: %d ", Guess);
printf("What do you think?");
scanf("%d", &feedback);
//guess was too low
if (feedback == -1)
{
Guess = Guess+1;
printf("my guess is: %d ", Guess);
printf("What do you think?");
scanf("%d", &feedback);
}
//guess was too high
else if (feedback == 1)
{
Guess = Guess-1;
printf("my guess is: %d ", Guess);
printf("What do you think?");
scanf("%d", &feedback);
}
else if (feedback == 0)
{
printf("I win!\n");
}
}
return 0;
}
輸出:
Enter a upper bound number: 40
Enter a lower bound number: 20
my guess is: 30 what do you think? -1
my guess is: 31 what do you think? -1
my guess is: 31 what do you think? -1
my guess is: 32 what do you think? -1
my guess is: 32 what do you think? -1
my guess is: 33 what do you think? -1
my guess is: 33 what do you think? -1
my guess is: 34 what do you think? -1
my guess is: 34 what do you think? -1
my guess is: 35 what do you think? 0
I win!
你快!..即將發表評論= 0 :) –