#include<stdio.h> int main() { int target, low=1,high=1000,guess, count=0; printf("请在1到1000之间选择一个数字:"); scanf("%d",&target); while(low<=high) guess=(low+high)/2; count++; printf("计算机猜测的数字为:%d,",guess); if (guess==target){ printf("猜对了!\n"); break; } eles if(guess<target){ printf("猜小了!\n"); low=guess+1; } else{ printf("猜大了!\n"); high=guess-1; } printf("输入你的反馈:1,猜小了;2,猜大了;3,猜对了。\n"); int feedback; scanf("%d",&feedback); if(feedback==3) { printf("猜对了,共用了%d次数猜测!\n",count); break; } } return 0; }为什么没发运行帮忙修改
时间: 2024-03-16 13:45:55 浏览: 24
你可能需要提供更多的信息,比如你运行这段程序时遇到了什么错误或者问题。另外,这段代码存在一些错误,可能会导致程序无法正常运行,例如 "else" 拼写错误,应该是 "else"。同时,应该将计数器 count 的初始化放在 while 循环内部。下面是修改后的代码,你可以参考一下:
```c
#include <stdio.h>
int main() {
int target, low = 1, high = 1000, guess, count = 0;
printf("请在1到1000之间选择一个数字:");
scanf("%d", &target);
while (low <= high) {
guess = (low + high) / 2;
count++;
printf("计算机猜测的数字为:%d,", guess);
if (guess == target) {
printf("猜对了!\n");
break;
} else if (guess < target) {
printf("猜小了!\n");
low = guess + 1;
} else {
printf("猜大了!\n");
high = guess - 1;
}
printf("输入你的反馈:1,猜小了;2,猜大了;3,猜对了。\n");
int feedback;
scanf("%d", &feedback);
if (feedback == 3) {
printf("猜对了,共用了%d次数猜测!\n", count);
break;
}
}
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)