1、
#include <stdio.h> int main(void) { char choice; printf("a) xxxx b) yyyy\n"); printf("c) zzzz q) quit.\n"); printf("input your choice: "); while((choice = getchar()) != 'q') { switch(choice) { case 'a': printf("choose a.\n"); break; case 'b': printf("choose b.\n"); break; case 'c': printf("choose c.\n"); break; default: printf("input errer, the range is a b c q\n"); break; } while(getchar() != '\n') continue; printf("\n\na) xxxx b) yyyy\n"); printf("c) zzzz q) quit.\n"); printf("input your choice: "); } return 0; }
2、
#include <stdio.h> char get_choice(void); int main(void) { char choice; while((choice = get_choice()) != 'q') { switch(choice) { case 'a': printf("you choose a.\n\n"); break; case 'b': printf("you choose b.\n\n"); break; case 'c': printf("you choose c.\n\n"); break; default: printf("grogramm ereor.\n\n"); break; } } return 0; } char get_choice(void) { char ch; printf("a) xxx b) yyyy\n"); printf("c) zzz q) quit\n"); printf("your choice: "); ch = getchar(); while(getchar() != '\n') continue; while(ch < 'a' || ch > 'c' && ch != 'q') { printf("the range is a b c and q, try again: "); ch = getchar(); while(getchar() != '\n') continue; } return ch; }
3、
#include <stdio.h> char get_choice(void); int main(void) { char choice; while((choice = get_choice()) != 'q') { switch(choice) { case 'a': printf("you choose a.\n\n"); break; case 'b': printf("you choose b.\n\n"); break; case 'c': printf("you choose c.\n\n"); break; default: printf("grogramm ereor.\n\n"); break; } } return 0; } char first_choice(void); char get_choice(void) { char ch; printf("a) xxx b) yyyy\n"); printf("c) zzz q) quit\n"); printf("your choice: "); ch = first_choice(); while(ch < 'a' || ch > 'c' && ch != 'q') { printf("the range is a b c and q, try again: "); ch = first_choice(); } return ch; } char first_choice(void) { char ch; ch = getchar(); while(getchar() != '\n') continue; return ch; }