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

请问这是什么原因呢?

你运行下看看


#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;
}

你的code有bug,,如果输入的不是 b或者 s,没有异常处理,会出现你所谓的 卡住了。

其实是 进入了 default case,然后一直while-defalut-while

可能你需要删掉35,55,76,我无法看出它们的意义

你写的代码我觉得错误的点在于 swich代码块中的getchar赋予的变量值是局部变量,当你输入y时,它只会无限break;
感觉你这个代码好多,python做了一下,这个思路,你转成C语言试试。
#l,r=1,100

while True:#无限循环

m=(l+r)//2

print('请问你猜得数字是不是%d' %(m))

a=int(input('1.是,2.大了,3.小了:'))

if a==1:

print('欧耶,恭喜你答对了!')

break

elif a==2:

r=m

else:

l=m

本地没有复现你的问题,可以正常输出。

建议将fflush函数放在scanf之前

建议你加个退出条件 start>=end 就退出,因为已经猜到了。没必要一直傻傻的循环


default: 
                      printf("Input is invalid option.\n");
                      printf("Pls input again:");
                      judge=getchar();
                    getchar();
                      break; 

二分法还有问题:

case ‘b’:
if (judge=='s')
                       {
                           end=a;
                       }else end=guess;

case ‘c’:
if(judge=='b') start=x;
                       else start=guess;

加上else,更新参数


case 'b':  a=guess;
                       guess=center(start,guess);               
                       printf("The integer i guess is %d.\n",guess);
                       if(guess-start<=1)
                       {
                           judge='y';
                           break;
                       } 
                       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;
                       }else end=guess;
                       
                       break;
           case 's':  x=guess;
                      guess=center(guess,end);   
                      printf("The integer i guess is %d.\n",guess);
                      if(end-guess<=1)
                      {
                          judge='y';
                          break;
                      } 
                       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;
                       else start=guess;
                       break;  

加了自动退出机制

你的这个程序 有问题:
正确的写法应该是:
printf("Please determine if my guess is equal to the true value,and corresponding to the input 'y' 'b' 's'.\n");
getchar();
judge=getchar();
由于printf输出了一个换行符,所以需要getchar()接收下,否则你是无法judge获取b值得,之前的写法b值被丢掉了,获取了个换行符赋给了judge,
下面有关
printf("\n")里有换行的如果下面紧接着需要getchar()字符的都需要先空调用一下getchar()接收换行,然后再judge=getchar()

有用的话麻烦您采纳一下