【发布时间】:2017-07-22 09:56:29
【问题描述】:
所以这个程序应该猜一个你在脑海中想到的数字。但是如果你运行它并在问你第一个问题后看到它不再需要输入。
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Enter the upper limit for the range of numbers:\n");
int n;
scanf("%d", &n);
int low=0;
int high=n;
printf("Answers should only be y for yes or n for no.\n");
do{
int median;
median=(high+low)/2;
printf("Is your number greater than %d median?\n", median);
char ans;
scanf("%c", &ans);
if(ans=='y' || ans=='Y'){
low=median;
continue;
}
else{
printf("Is your number smaller than %d median?\n", median);
scanf("%c", &ans);
if(ans=='y' || ans=='Y'){
high = median;
continue;
}
else
{
low = high = median;
}
}
}while(low != high);
printf("Your number is: %d\n", low);
return 0;
}
【问题讨论】:
-
那我该如何解决呢?
-
这个问题有 10 个 (!) 答案。
-
正如 Eugene Sh 所说,您可以在 stackoverflow 本身上找到很多问题的答案。但是,如果您仍然想在现有代码中修复它,请使用 scanf("%*c%c", &ans);看看:stackoverflow.com/questions/1669821/…
标签: c