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