do
{
randomNum = (topBorder + lowBorder)/2;
printf("Your number is: %d?\n",randomNum);
printf("My number is(larger-l/smaller-s/correct-c): ");
scanf("%c", &compare);
printf("\n");
if (compare == 's') {
topBorder = randomNum;
}
else if (compare == 'l') {
lowBorder=randomNum;
}
else if (compare == 'c') {
printf("I like that you take my number.\n");
return 0;
}
} while (compare = 1);
程序工作正确,但我不知道为什么写双倍时间*您的号码是:(号码)我的号码是(...):
./pcIsUser
Please, guess number between 1 and 100.
Your number is: 50?
My number is(larger-l/smaller-s/correct-c): l
***Your number is: 75? My number is(larger-l/smaller-s/correct-c):***
Your number is: 75?
My number is(larger-l/smaller-s/correct-c): s
***Your number is: 62?
My number is(larger-l/smaller-s/correct-c):***
Your number is: 62?
My number is(larger-l/smaller-s/correct-c): c
I like that you take my number.
答案 0 :(得分:1)
问题
while (compare = 1)
它正在为1
分配compare
。
你需要
while (compare == 1)
并且您永远不会从1
获得scanf()
,所以也许
while (compare)
也有效,
scanf("%c", &compare);
正在'\n'
中的前一个stdin
,因此
scanf(" %c", &compare);
会明确地跳过空格并解决问题。
您还应该check
scanf()
的返回值,以确保在Linux上按 Ctrl + D 发送EOF
的人没有意外发生或< Windows上的kbd> Ctrl + Z 。