if-else 回顾
本课属于 Coddy C# 学习旅程的“基础知识”单元。第 29 课,共 69 课。
挑战
入门给你一段代码,它会接收两个数字 n1 和 n2,以及一个单字符字符串 op 作为输入。
注意:我们会在后续课程中学习如何从用户那里获取输入,目前先不要改动前三行。
op 的可能值为 '+'、'-'、'/' 和 '*'
你的任务是根据条件设置变量 result:
- 如果
op是'+',将result设为n1 + n2。 - 如果
op是'-',将result设为n1 - n2。 - 如果
op是'/',将result设为n1 / n2。 - 如果
op是'*',将result设为n1 * n2。
亲手试试
using System;
public class Program {
public static void Main(string[] args) {
int n1 = int.Parse(Console.ReadLine());
int n2 = int.Parse(Console.ReadLine());
string op = Console.ReadLine();
// 在下方编写你的代码
double result = 0;
Console.WriteLine(result);
}
}基础知识中的所有课
自己练习: C# 在线编译器