C#练习题VS2019
时间: 2024-02-05 12:08:13 浏览: 186
以下是一个C#练习题的例子,你可以在VS2019中尝试编写和运行它:
```csharp
using System;
namespace Exercise
{
class Program
{
static void Main(string[] args)
{
// 填空题
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
Console.WriteLine("The sum is: " + sum);
// 选择题
int x = 5;
int y = 10;
int z = 15;
int result = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);
Console.WriteLine("The largest number is: " + result);
// 判断题
int a = 5;
int b = 10;
bool isGreater = (a > b);
Console.WriteLine("Is a greater than b? " + isGreater);
// 编程题
int[] numbers = { 1, 2, 3, 4, 5 };
int sumOfNumbers = 0;
foreach (int number in numbers)
{
sumOfNumbers += number;
}
Console.WriteLine("The sum of numbers is: " + sumOfNumbers);
}
}
}
```
这个例子包含了填空题、选择题、判断题和编程题。你可以在VS2019中创建一个新的C#控制台应用程序项目,将上述代码复制到`Program.cs`文件中,并运行该程序来查看结果。
阅读全文