yuchu3d 2021-11-04 09:47 采纳率: 80%
浏览 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 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项