C#第五讲 函数的用法

cs 复制代码
using System.Windows;

namespace test7
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            eslre c = new eslre();
            int x = c.add(3, 5);
            string now = c.Today();
            MessageBox.Show($"{x}\n{now}","显示结果");
            c.PrintSum(3, 6);
        }
        

    }

    class eslre
    {

        public int add(int a,int b)//有输入 有输出
        {
            int result = a + b;
            return result;
        }
        public string Today()//无输入 有输出
        {
            DateTime chinaTime = DateTime.UtcNow.AddHours(8);
            return chinaTime.ToString("yyyy-MM-dd HH:mm:ss");
        }
        public void PrintSum(int a,int b)//有输入 无输出
        {
            int result = a + b;
            MessageBox.Show(result.ToString(),"显示结果");
        }

    }
    

}

二基础算法

cs 复制代码
using System.Windows;
using System;

namespace test7
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            eslre c = new eslre();
            c.PrintXTo1(10);
            c.PrintXTo2(3);
        }
        

    }

    class eslre
    {
        public void PrintXTo1(int x)//遍历打印
        {
            for (int i = x; i>=0; i--)
            {
                Console.WriteLine(i);
            }
        }
        public void PrintXTo2(int x)//递归打印
        {
            if (x==1)
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine(x);
                PrintXTo2(x - 1);
            }

        }
    }
}

三从1加到100的三种算法

cs 复制代码
using System.Windows;
using System;

namespace test7
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            eslre c = new eslre();
        }
        

    }

    class eslre
    {
        //public void PrintXTo1(int x)//遍历打印
        //{
        //    for (int i = x; i>=0; i--)
        //    {
        //        Console.WriteLine(i);
        //    }
        //}
        //public void PrintXTo2(int x)//递归打印
        //{
        //    if (x==1)
        //    {
        //        Console.WriteLine(x);
        //    }
        //    else
        //    {
        //        Console.WriteLine(x);
        //        PrintXTo2(x - 1);
        //    }

        //}
        public int SumFrom1ToX1(int x)//for循环从1加到100
        {
            int result = 0;
            for (int i = 1; i <= x; i++)
            {
                result = result + i;
            }
            return result;
        }
        public int SumFrom1ToX2(int x)//递归循环从1加到100
        {
            if (x == 1)
            {
                return 1;
            }
            else
            {
                int result = x + SumFrom1ToX2(x - 1);
                return result;
            }
        }
        public int SumFrom1ToX3(int x)//高斯算法从1加到100
        {
            return (x + 1)*x/2;
        }
    }
}
相关推荐
励志不掉头发的内向程序员14 小时前
【LibreCAD 2D架构】鼠标点下的坐标为什么会被“吸”走?LibreCAD 对象捕捉系统解析
开发语言·c++·qt·学习·系统架构
Generalzy14 小时前
像 gofmt 一样格式化 Python:Black、Ruff、YAPF、autopep8 谁才是 2026 年的首选?
开发语言·python
朝朝辞暮i15 小时前
C++第一课
开发语言·c++·算法
李少兄15 小时前
JavaScript 对象完全指南
开发语言·javascript·ecmascript
张小姐的猫15 小时前
【AI大模型接入SDK】 —— Ollama本地接入Deepseek
java·linux·开发语言·网络·c++·人工智能
Nuanyt15 小时前
JUC常见核心知识梳理01 线程 并发 JMM volatile 管程 锁 synchronized
java·开发语言·网络·jvm
adaierya15 小时前
用 AI 解决音频转换编程问题
开发语言·人工智能·python·分类·ai编程
何何____15 小时前
web方向js考核题解
开发语言·前端·javascript
ThornArmor15 小时前
重铸1996|首秀点亮:编写 helloworld 并用 Makefile 自动化收割第一个 .z64 卡带镜像
开发语言
船厂电气自动化ai大模型15 小时前
AI大模型与数学 第73课 秩的核心定义、矩阵秩的通用求解方法
开发语言·数据结构·机器学习