| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 844 人关注过本帖, 1 人收藏
标题:【求助】关于switch的问题!
只看楼主 加入收藏
OneMan
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-12-5
结帖率:100%
收藏(1)
 问题点数:0 回复次数:9 
【求助】关于switch的问题!
#include<stdio.h>
void main()
{
  int choice;
head:
  printf("\t\t+---------------------+-------------------------+\n");
  printf("\t\t|     (1)苹果         |     (2)西瓜             |\n");
  printf("\t\t+---------------------+-------------------------|\n");
  printf("\t\t|     (3)exit();      |                         |\n");
  printf("\t\t+---------------------+-------------------------+\n");
 again:
  printf("input your choice:");
  scanf("%d",&choice);
  switch(choice){
  
  case 1:printf("你选的是苹果\n");goto head;break;
  case 2:printf("你选的是西瓜\n");goto head;break;
  case 3:exit(0);
  default:printf("你输入有误,请重试:\n");goto again;break;
   }
}

这里有个问题,就是我输入数字键不是1,2,3时会提示出错,并且重试,goto到again,可是为什么我输入不是数字,而是字符键程序会进入死循环,这是为什么???
搜索更多相关主题的帖子: switch 
2008-12-06 19:41
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1687
专家分:4253
注 册:2007-12-27
收藏
得分:0 
因为字符没办法按照字面意思解释为十进制整数

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2008-12-06 19:45
OneMan
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-12-5
收藏
得分:0 
还是不懂,程序不是已经运行到default那里了嘛,应该是其他数字一样的,为什么它就会无限输入input your choice(无限滚屏)而不让我输入呢?
2008-12-06 19:50
bestone
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-11-24
收藏
得分:0 
#include<stdio.h>
void main()
{
  int choice;
head:
  printf("\t\t+---------------------+-------------------------+\n");
  printf("\t\t|     (1)苹果         |     (2)西瓜             |\n");
  printf("\t\t+---------------------+-------------------------|\n");
  printf("\t\t|     (3)exit();      |                         |\n");
  printf("\t\t+---------------------+-------------------------+\n");
again:
  printf("input your choice:");
  scanf("%d",&choice);
  switch(choice){
  
  case 1:printf("你选的是苹果\n");goto head;break;
  case 2:printf("你选的是西瓜\n");goto head;break;
  case 3:printf("%d\n",'0');
  default:printf("你输入有误,请重试:\n");goto again;break;
   }
}

你写的调试时显示exit是错误的 我改成printf("%d\n",'0');程序能够按照理想的方式去运行了 显示如下
                +---------------------+-------------------------+
                |     (1)苹果         |     (2)西瓜             |
                +---------------------+-------------------------|
                |     (3)exit();      |                         |
                +---------------------+-------------------------+
input your choice:1
你选的是苹果
                +---------------------+-------------------------+
                |     (1)苹果         |     (2)西瓜             |
                +---------------------+-------------------------|
                |     (3)exit();      |                         |
                +---------------------+-------------------------+
input your choice:2
你选的是西瓜
                +---------------------+-------------------------+
                |     (1)苹果         |     (2)西瓜             |
                +---------------------+-------------------------|
                |     (3)exit();      |                         |
                +---------------------+-------------------------+
input your choice:3
48
你输入有误,请重试:
input your choice:
2008-12-06 22:53
seaisland
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-11-27
收藏
得分:0 
是scanf函数出了问题!
程序调用scanf函数(其他输入函数也一样)时,程序的所有输入会被暂时存放在所谓的“流”结构中,所谓流就是字节序列,scanf函数顺序地从流中取出字符并转换,如果转换正确,则将取出的字符丢弃,如果不能正确转换,则将取出的字符留在流中原来的位置。scanf函数下次处理输入时仍然会从原先那个没有转换成功的字符处提取字符,并试图转换,但是失败,那个“坏字符”还留在流中原来位置,等到scanf再次处理输入时,又会转换不成功。这样导致程序进入死循环。
2008-12-06 23:22
OneMan
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-12-5
收藏
得分:0 
恩,谢谢楼上的解答,在下理解了,非常非常感谢!!
2008-12-06 23:45
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
楼主,写程序时尽量避免使用 goto ,那样会使程序很不流畅。下面我试作了一个小程序,供您参考。

程序代码:
#include <stdio.h>
#define CLS system("cls")
int main(void)
{
    int a;
    char c;
    while(1)
    {
        do
        {
            printf("\t\t+---------------------+-------------------------+\n");
            printf("\t\t|     (1)苹果         |     (2)西瓜             |\n");
            printf("\t\t+---------------------+-------------------------|\n");
            printf("\t\t|     (3)exit();      |                         |\n");
            printf("\t\t+---------------------+-------------------------+\n");
            printf("\n\ninput a:\n");
            scanf("%d",&a);
            while(c=getchar()!='\n');
            CLS;
        }while(a!=1&&a!=2&&a!=3);
        switch(a)
        {
            case 1:printf("\n\n\tapple\n\n");break;
            case 2:printf("\n\n\twatermelon\n\n");break;
            case 3:exit(0);
        }
    }
    return 0;
}



2008-12-06 23:57
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
PS
既然你在 case 中使用了 goto ,转到别处去了,那么后面也不用使用 break 了,那样也是多余的,不会运行到那里的。
2008-12-06 23:59
吻柔
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-15
收藏
得分:0 
这样写应该没问题
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
while(1){
fflush(stdin);
  printf("\t\t+---------------------+-------------------------+\n");
  printf("\t\t|     (1)苹果         |     (2)西瓜             |\n");
  printf("\t\t+---------------------+-------------------------|\n");
  printf("\t\t|     (3)exit();      |                         |\n");
  printf("\t\t+---------------------+-------------------------+\n");
  printf("input your choice:");
 scanf("%d",&choice);
if(getchar()!='\n')choice=-1;//避免输入数字+字母格式
 switch(choice){
  
  case 1:printf("你选的是苹果\n");break;
  case 2:printf("你选的是西瓜\n");break;
  case 3:exit(0);
  default:printf("你输入有误,请重试:\n");}
choice=-1;
}
}
2008-12-07 00:24
OneMan
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-12-5
收藏
得分:0 
非常感谢大家的讲解,谢谢!!
2008-12-07 15:46
快速回复:【求助】关于switch的问题!
数据加载中...
 
   



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

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