这个实例很简单,看一下就能明白,至于我已经对C比较熟悉了,为什么还要从这么简单的例子入手,这个需要再详细的说明一下。由于之前学习C的时候,就是急功近利,没有仔细的去品味C中,特别是指针中的一些乐趣,所以我选择从基础再学习一遍,就这样咯。
#include <stdio.h>
/**
* 实现自动取款机界面的模拟来学习使用switch语句
* switch...case语句的结构
* switch(int类型变量){
* case 1: //如果是1,进行相应的处理
* ....
* break;
* case 2:
* ....
* break;
* default: //可有可无的,表示如果不是上面的所有
* ....
* break;
* }
*/
int main(void)
{
do{
printf("===================================\n");
printf(": Please select the key: :\n");
printf(": 1: Query :\n");
printf(": 2: Credit :\n");
printf(": 3: Debit :\n");
printf(": 4: Return :\n");
printf("===================================\n");
int selected = 0;
scanf("%d",&selected);
switch(selected){
case 1:
printf("===================================\n");
printf(": Your balance is $1000 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 2:
printf("===================================\n");
printf(": Please select credit money :\n");
printf(": 1: $50 :\n");
printf(": 2: $100 :\n");
printf(": 3: return :\n");
printf("===================================\n");
int credit = 0;
scanf("%d",&credit);
switch(credit){
case 1:
printf("===================================\n");
printf(": Your Credit money is $50 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 2:
printf("===================================\n");
printf(": Your Credit money is $100 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 3:
break;
default:
printf("===================================\n");
printf(": Op error!! :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
}
break;
case 3:
printf("===================================\n");
printf(": Please select debit money :\n");
printf(": 1: $50 :\n");
printf(": 2: $100 :\n");
printf(": 3: $500 :\n");
printf(": 4: $1000 :\n");
printf(": 5: return :\n");
printf("===================================\n");
int debit = 0;
scanf("%d",&debit);
switch(debit){
case 1:
printf("===================================\n");
printf(": Your debit money is $50 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 2:
printf("===================================\n");
printf(": Your debit money is $100 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 3:
printf("===================================\n");
printf(": Your debit money is $500 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 4:
printf("===================================\n");
printf(": Your debit money is $1000 :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
case 5:
break;
default:
printf("===================================\n");
printf(": Op error!! :\n");
printf(": Please enter any key to return :\n");
printf("===================================\n");
getch();
break;
}
break;
case 4:
printf("===================================\n");
printf(": Thank you for your using :\n");
printf(": Good Bye :\n");
printf("===================================\n");
return;
default:
return;
}
}while(1);
return 0;
}
时间: 2024-12-12 18:12:10