提交 fe188598 编写于 作者: F feilong

增加C#第8题

上级 226c1f86
{
"type": "code_options",
"author": "huanhuilong",
"source": "Swtich.md"
}
\ No newline at end of file
# 多条件分支
以下C#代码的输出是多少?
```csharp
int Calc(char op, int a, int b){
if(op=='+'){
return a+b;
}else if(op=='-'){
return a-b;
}else if(op=='*'){
return a*b;
}else if(op=='/'){
try{
return a/b;
}catch(DivideByZeroException e){
throw new Exception("被除数不能为0");
}
}else{
throw new Exception("无效的操作符");
}
}
int ret = Calc('*', Calc('+',1, Calc('-',3,Calc('/',1,2))), Calc('-',3,Calc('/',1,2)));
Console.WriteLine("ret={0}", ret);
```
## 答案
```csharp
ret=12
```
## 选项
### A
```csharp
ret=10
```
### B
```csharp
Unhandled exception. System.Exception: 无效的操作符
```
### C
```csharp
Unhandled exception. System.Exception: 被除数不能为0
```
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [ "export": [
"Guard.json" "Guard.json",
"Switch.json"
] ]
} }
\ No newline at end of file
...@@ -66,7 +66,7 @@ void Path3(bool a, bool b, bool c, bool d){ ...@@ -66,7 +66,7 @@ void Path3(bool a, bool b, bool c, bool d){
Console.WriteLine(PathValue(a,b,c,d)); Console.WriteLine(PathValue(a,b,c,d));
} }
void Path3(bool a, bool b, bool c, bool d){ void Path(bool a, bool b, bool c, bool d){
if(a){ if(a){
if(b){ if(b){
if(c){ if(c){
...@@ -87,4 +87,26 @@ void Path3(bool a, bool b, bool c, bool d){ ...@@ -87,4 +87,26 @@ void Path3(bool a, bool b, bool c, bool d){
} }
} }
Path(true,true,false,false); // Path(true,true,false,false);
\ No newline at end of file
int Calc(char op, int a, int b){
if(op=='+'){
return a+b;
}else if(op=='-'){
return a-b;
}else if(op=='*'){
return a*b;
}else if(op=='/'){
try{
return a/b;
}catch(DivideByZeroException e){
throw new Exception("被除数不能为0");
}
}else{
throw new Exception("无效的操作符");
}
}
int ret = Calc('*', Calc('+',1, Calc('-',3,Calc('/',1,0))), Calc('-',3,Calc('/',1,2)));
Console.WriteLine("ret={0}", ret);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册