| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 573 人关注过本帖
标题:一个do while 问题
只看楼主 加入收藏
aluesnow
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-14
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:7 
一个do while 问题
小弟刚开始学习C语言,请问各位我的程序执行到printf("\n Want to play again?y/n:");为什么会直接跳出程序 ,而不再要求我输入Y/y
#include "stdio.h"
main()
{
    int n;
    char x;
    void hanoi(int n,char a,char b,char c);
    printf("\n *********************************************************");
    printf("\n * * *     Program for simulating the solution       * * *");
    printf("\n * * *       of the game of tower of hanoi,          * * *");
    printf("\n *********************************************************");
    printf("\n =========================================================");
    do{
        printf("\n Please enter the number of discs to be moved:");
        scanf("%d",&n);
        hanoi(n,'a','b','c');
       printf("\n Want to play again?y/n:");
        scanf("%c",&x);

    }while(x=='y'||x=='Y'); /*为什么不能循环*/
}

void hanoi(int n,char a,char b,char c)    /* 汉诺塔问题,永远的递归*/
{
    if(n>0)
    {
        hanoi(n-1,a,c,b);
        printf("\n Move disc %d form pile %c to %c",n,a,b);
        hanoi(n-1,c,b,a);
    }
}
搜索更多相关主题的帖子: solution include C语言 
2010-06-25 11:03
aluesnow
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-14
收藏
得分:0 
请问是windows7操作系统的事还是其他什么别的原因?
在论坛看了一个帖子,别人都说没问题,但是我运行的话还是出现跟我那个同样的问题,即不执行输入Y的那个语句:
#include <stdio.h>
main()
{
      int temp;
      float celsius;
      char repeat;
      do
      {
           printf("Input a temperature:");
           scanf("%d", &temp);
           celsius=(5.0/9.0)*(temp-32);
           printf("The converted F format is %.2f\n", celsius);
           printf("Do you have other data need to be converted?\n");
           printf("Press Y or y if you want to continue");
           repeat=getchar();
           putchar('\n');
           }
      while (repeat=='Y' || repeat=='y');
      
      }
2010-06-25 11:21
mojianfei
Rank: 1
等 级:新手上路
帖 子:2
专家分:3
注 册:2010-5-14
收藏
得分:3 
         getchar();
        scanf("%c",&x);
        if(x=='n'||x=='N')exit(0);
在scanf前面加getchar();吃掉上次的那个回车就可以了.
2010-06-25 11:46
呵呵呵。。
Rank: 2
等 级:论坛游民
帖 子:43
专家分:52
注 册:2010-6-11
收藏
得分:3 
#include <stdio.h>
#include<string.h>
main()
{
      int temp;
      float celsius;
      char repeat;
      do
      {
           printf("Input a temperature:");
           scanf("%d", &temp);
           celsius=(5.0/9.0)*(temp-32);
           printf("The converted F format is %.2f\n", celsius);
           printf("Do you have other data need to be converted?\n");
           printf("Press Y or y if you want to continue\n");
           repeat=getchar();  //如果系统自动加了个回车,就会执行if语句
           if(repeat=='\n')   
               printf("aaaaa\n");
           putchar('\n');
           }while (repeat=='Y' || repeat=='y');
}
我用的是XP系统,我也不知道为什么系统会自动加了个回车
2010-06-25 12:49
dstone
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:20
专家分:125
注 册:2010-6-17
收藏
得分:4 
scanf从缓冲区读入字符,你输入n的值后,输入一个回车,赋给了x,所以不循环,在scanf(“%c”,&x)之前加一个getchar(),程序就正确了
 
2010-06-25 13:44
aluesnow
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-14
收藏
得分:0 
谢谢大家,问题是这样的,解决了。  还有一个问题,以后如果想回车之后在接收字符,是不是必须得 加上getchar();?
2010-06-25 14:24
dstone
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:20
专家分:125
注 册:2010-6-17
收藏
得分:0 
你要理解缓冲区的原理,这个问题的本质。把scanf()、getchar()、getch()等函数的用法看看,理解下输入、输出流的概念,看过了,你就知道以后怎么做了
2010-06-25 14:48
aluesnow
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-14
收藏
得分:0 
好的 谢谢  我去看看~~
2010-06-25 14:52
快速回复:一个do while 问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017844 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved