yuchu3d 2021-11-04 09:47 采纳率: 82.8%
浏览 294
已结题

c语言 while循环 每次循环7次之后就停在那里进行不下去了

下面的程序就是一个猜数字的游戏,在0-100间的数字,内定一个数字, 然后猜数字,通过交互输入 ,程序最终通过二分法找到我们需要的正确数字。

下面的程序问题应该不是很大, 问题是 每次 只循环7次 ,也就是 输入的次数 达到7次之后,程序就停在那里了 没反应了。
程序如下:

#include<stdio.h>
int center(int start,int end);

int main(void)

{
    int guess,start=0,end=100,a,x;
    char judge;
    
    printf("Please guess a integer which is between 0 and 100,after i guess ,\n "
    "please enter 'y' or 'w' which means right or wrong. if i guess bigger than\n"
    "the true value,please enter 'b', if samller ,enter 's'.\n");
    
    printf("Now i start guess, i guess the integer is:\n");
    
    scanf("%d",&guess);
    
    fflush(stdin);
    
    while (guess<0||guess>100)
    {
      
      printf("You enter wrong integer which is should between 0 and 100, please enter again:\n");
      
      scanf("%d",&guess);    
      fflush(stdin);
    }
    
    printf("The integer i guess is %d.\n",guess);
    
    printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
    
    judge=getchar();
    
    getchar();
    
    if(judge=='y')
    
    printf("You are right, your guess is %d which is equal to the true value.\n",guess);
    
    while(judge!='y')
    {
        switch(judge)
        {
            case 'b':  a=guess;
            
                       guess=center(start,guess);                       
                   
                       printf("The integer i guess is %d.\n",guess);
            
                       printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
    
                       judge=getchar();
                       
                       getchar(); 
                       
                       if (judge=='s')
                       {
                           
                           end=a;
                       }
                                                            
                       
                       break;
                       
           case 's':  x=guess;
           
                      guess=center(guess,end);               
                      
                      printf("The integer i guess is %d.\n",guess);
                      
                       printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
    
                       judge=getchar();
                       
                       getchar();
                       
                       if(judge=='b') start=x;
                       
                       break;  
                       
          default: break;          
        }
        
            
        
    }
    
    printf("You are right, your guess is %d which is equal to the true value.\n",guess);
    
    return 0;
    
    
    
    
}

int center(int start,int end)
{
    int result1;
    
    result1=(start+end)/2;
    
    return result1;
}


例如 我们假定 真实的数字是 38, 初次输入猜测的数字是 52,

运行的结果如下图:

img

请问这是什么原因呢?

  • 写回答

11条回答 默认 最新

  • soar3033 2021-11-04 10:20
    关注

    你运行下看看

    
    #include<stdio.h>
    int center(int start, int end);
    int main(void)
    {
        int guess, start = 0, end = 100, a, x;
        char judge;
        printf("Please guess a integer which is between 0 and 100,after i guess ,\n "
            "please enter 'y' or 'w' which means right or wrong. if i guess bigger than\n"
            "the true value,please enter 'b', if samller ,enter 's'.\n");
        printf("Now i start guess, i guess the integer is:\n");
        scanf("%d", &guess);
        fflush(stdin);
        while (guess < 0 || guess>100)
        {
            printf("You enter wrong integer which is should between 0 and 100, please enter again:\n");
            scanf("%d", &guess);
            fflush(stdin);
        }
        getchar();
        printf("The integer i guess is %d.\n", guess);
        printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
        judge = getchar();
        getchar();
        if (judge == 'y')
            printf("You are right, your guess is %d which is equal to the true value.\n", guess);
        while (judge != 'y')
        {
            switch (judge)
            {
            case 'b':  a = guess;
                guess = center(start, guess);
                printf("The integer i guess is %d.\n", guess);
                printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
                judge = getchar();
                getchar();
                if (judge == 's')
                {
                    end = a;
                }
    
                break;
            case 's':  x = guess;
                guess = center(guess, end);
                printf("The integer i guess is %d.\n", guess);
                printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b'  's'.\n");
                judge = getchar();
                getchar();
                if (judge == 'b') start = x;
                break;
            default: break;
            }
    
        }
        printf("You are right, your guess is %d which is equal to the true value.\n", guess);
        return 0;
    
    
    }
    int center(int start, int end)
    {
        int result1;
        result1 = (start + end) / 2;
        return result1;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 已采纳回答 11月4日
  • 赞助了问题酬金 11月4日
  • 修改了问题 11月4日
  • 展开全部

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算