1. Switch /Case
Switch (){
cases:
break;
default:
:
要求写一个简易的计算器程序,实现简单的加减乘除计算,并输出结果。
例如: 输入 1+1 输出结果应该为2
当输入1 、1时要显示 输入错误
程序如下:
#include <stdio.h>
int main(){
int operation1, operation2, result;
char operater;
scanf("%d%c%d", &operation1, &operater, &operation2);
switch (operater){
case '+':
result = operation1 + operation2;
break;
case '-':
result = operation1 - operation2;
break;
case '*':
result = operation1 * operation2;
break;
case '/':
result = operation1 / operation2;
break;
default:
printf("Error");
}
if(operater == '+' || operater == '-' || operater == '*' || operater == '/' ){
printf("%d",result);
}
return 0;
}
错误总结:
switch() continue 结束本次循环 break 跳出整个循环
1. 错误1 case:忘记写break;
如果case1 忘记写break, 则它会执行下一条case的结果。
2. 错误2
operater == '+' || operater == '-' || operater == '*' || operater == '/'
|| 表示逻辑或 |表示按位或