if-else 回顾
本课属于 Coddy C# 学习旅程的“基础知识”单元。第 29 课,共 70 课。
挑战
入门给你一段代码,它会接收两个数字 n1 和 n2,以及一个单字符字符串 op 作为输入。
注意:我们会在后续的课中学习如何获取用户输入,目前先不要改动前三行。
op 的可能值为 "+"、"-"、"/" 和 "*"。op 是一个 string,所以它的值要用双引号括起来。
你的任务是根据条件设置变量 result:
- 如果
op是"+",就将n1 + n2赋给result。 - 如果
op是"-",就将n1 - n2赋给result。 - 如果
op是"/",就将n1 / n2赋给result。 - 如果
op是"*",就将n1 * n2赋给result。
亲手试试
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# 在线编译器