404 Not Found

404 Not Found


nginx

C#示例集

FizzBuzz

遍历1到100,3的倍数输出Fizz,5的倍数输出Buzz,同时是3和5的倍数输出FizzBuzz,否则输出数字本身。

示例

CSHARP
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 100; i++)
        {
            if (i % 15 == 0)
                Console.WriteLine("FizzBuzz");
            else if (i % 3 == 0)
                Console.WriteLine("Fizz");
            else if (i % 5 == 0)
                Console.WriteLine("Buzz");
            else
                Console.WriteLine(i);
        }
    }
}
▶ 试一试
TEXT
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...
98
Fizz
Buzz

斐波那契数列

输出斐波那契数列的前20个数,每个数是前两个数之和。

示例

CSHARP
using System;

class Program
{
    static void Main()
    {
        int a = 0, b = 1;
        for (int i = 0; i < 20; i++)
        {
            Console.Write(a + " ");
            int temp = a;
            a = b;
            b = temp + b;
        }
        Console.WriteLine();
    }
}
▶ 试一试
TEXT
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

九九乘法表

打印经典的九九乘法表,呈三角形排列。

示例

CSHARP
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}x{i}={i * j}\t");
            }
            Console.WriteLine();
        }
    }
}
▶ 试一试
TEXT
1x1=1
1x2=2   2x2=4
1x3=3   2x3=6   3x3=9
1x4=4   2x4=8   3x4=12  4x4=16
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25
1x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=36
1x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49
1x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64
1x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81

素数判断

判断用户输入的正整数是否为素数,并输出结果。

示例

CSHARP
using System;

class Program
{
    static bool IsPrime(int n)
    {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0) return false;
        }
        return true;
    }

    static void Main()
    {
        int number = 29;
        if (IsPrime(number))
            Console.WriteLine($"{number} 是素数");
        else
            Console.WriteLine($"{number} 不是素数");

        number = 35;
        if (IsPrime(number))
            Console.WriteLine($"{number} 是素数");
        else
            Console.WriteLine($"{number} 不是素数");
    }
}
▶ 试一试
TEXT
29 是素数
35 不是素数

回文检测

判断一个字符串是否为回文(正读反读相同)。

示例

CSHARP
using System;

class Program
{
    static bool IsPalindrome(string s)
    {
        int left = 0, right = s.Length - 1;
        while (left < right)
        {
            if (s[left] != s[right]) return false;
            left++;
            right--;
        }
        return true;
    }

    static void Main()
    {
        string[] tests = { "racecar", "hello", "level", "world" };
        foreach (string t in tests)
        {
            Console.WriteLine($"{t} -> {(IsPalindrome(t) ? "是回文" : "不是回文")}");
        }
    }
}
▶ 试一试
TEXT
racecar -> 是回文
hello -> 不是回文
level -> 是回文
world -> 不是回文

反转字符串

将输入字符串反转并输出结果。

示例

CSHARP
using System;

class Program
{
    static string ReverseString(string s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

    static void Main()
    {
        string original = "Hello CSharp";
        string reversed = ReverseString(original);
        Console.WriteLine($"原始: {original}");
        Console.WriteLine($"反转: {reversed}");
    }
}
▶ 试一试
TEXT
原始: Hello CSharp
反转: prahSC olleH

统计元音字母

统计字符串中元音字母(a、e、i、o、u,不区分大小写)出现的次数。

示例

CSHARP
using System;

class Program
{
    static int CountVowels(string s)
    {
        int count = 0;
        string vowels = "aeiouAEIOU";
        foreach (char c in s)
        {
            if (vowels.IndexOf(c) >= 0)
                count++;
        }
        return count;
    }

    static void Main()
    {
        string text = "Hello World Programming";
        int result = CountVowels(text);
        Console.WriteLine($"字符串: {text}");
        Console.WriteLine($"元音字母数: {result}");
    }
}
▶ 试一试
TEXT
字符串: Hello World Programming
元音字母数: 6

文件读写

演示基本的文本文件写入与读取操作。

示例

CSHARP
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "test.txt";
        string[] lines = { "第一行", "第二行", "第三行" };

        File.WriteAllLines(path, lines);
        Console.WriteLine("写入完成");

        string[] readLines = File.ReadAllLines(path);
        Console.WriteLine("读取内容:");
        foreach (string line in readLines)
        {
            Console.WriteLine(line);
        }

        File.Delete(path);
    }
}
▶ 试一试
TEXT
写入完成
读取内容:
第一行
第二行
第三行

猜数字游戏

程序随机生成1到100之间的整数,玩家通过输入猜测,程序提示偏大或偏小,直到猜中为止。

示例

CSHARP
using System;

class Program
{
    static void Main()
    {
        Random rnd = new Random(42);
        int target = rnd.Next(1, 101);
        int[] guesses = { 50, 75, 62, 68, 71, 73, 72 };
        int attempts = 0;

        Console.WriteLine($"目标数字: {target}(仅作演示显示)");
        Console.WriteLine("---");

        foreach (int guess in guesses)
        {
            attempts++;
            if (guess < target)
                Console.WriteLine($"第{attempts}次猜测: {guess} - 偏小");
            else if (guess > target)
                Console.WriteLine($"第{attempts}次猜测: {guess} - 偏大");
            else
            {
                Console.WriteLine($"第{attempts}次猜测: {guess} - 猜中了!");
                break;
            }
        }
    }
}
▶ 试一试
TEXT
目标数字: 72(仅作演示显示)
---
第1次猜测: 50 - 偏小
第2次猜测: 75 - 偏大
第3次猜测: 62 - 偏小
第4次猜测: 68 - 偏小
第5次猜测: 71 - 偏小
第6次猜测: 73 - 偏大
第7次猜测: 72 - 猜中了!

学生成绩计算

输入一组学生成绩,计算平均分、最高分和最低分。

示例

CSHARP
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        double[] scores = { 85.5, 92.0, 78.5, 96.0, 88.0, 73.5, 91.0 };

        double average = scores.Average();
        double max = scores.Max();
        double min = scores.Min();

        Console.WriteLine("学生成绩: " + string.Join(", ", scores));
        Console.WriteLine($"平均分: {average:F1}");
        Console.WriteLine($"最高分: {max:F1}");
        Console.WriteLine($"最低分: {min:F1}");
    }
}
▶ 试一试
TEXT
学生成绩: 85.5, 92, 78.5, 96, 88, 73.5, 91
平均分: 86.4
最高分: 96.0
最低分: 73.5
Web-Tutorial.com

Web-Tutorial 技术团队

由多位开发者共同维护的编程教程平台。每篇教程由对应领域的开发者编写和审核,确保内容准确可靠。如发现任何问题,欢迎向我们反馈。

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏