C语言输入选择问题

如果有下面的代码
int select;
printf("1 for xxxx");
printf("2 for xxxx");
printf("3 for xxxx");
printf("please input you select:");
scanf("%d",&select);

如果用户输入的是1,2,3以外的字符或数字,就要重新输入,那么下面的代码应该怎么写?

#include <stdio.h>

int main()

{

 int select;

 printf("1 for xxxx\n");

 printf("2 for xxxx\n");

 printf("3 for xxxx\n");

 printf("please input you select:\n");

 scanf("%d",&select); 

 loop:

 switch(select){

  case 1 : printf("1 for xxxx\n"); break;

  case 2 : printf("2 for xxxx\n"); break;

  case 3 : printf("3 for xxxx\n"); break;

  default : printf("please input you select again\n");

            scanf("%d",&select);

            goto loop;

   }

 return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-06
这里select的类型应该改成char,否则输入字符时将进入死循环。

char select;
printf("1 for xxxx");
printf("2 for xxxx");
printf("3 for xxxx");
printf("please input you select:");
do
scanf("%c",&select);
while (select < '1' || select > '3');

switch (select)
{
case '1':
break;
case '2':
break;
case '3':
break;
default:
break;
}本回答被提问者采纳
第2个回答  2009-06-06
while(select>3||select<1){
scanf("%d",&select);
}

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网