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日
  • 展开全部

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答