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;
        }
    }
}
相关推荐
SmartRadio6 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
kylezhao20196 小时前
C#winform数据绑定
c#
rfidunion6 小时前
QT5.7.0编译移植
开发语言·qt
rit84324996 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
大、男人6 小时前
python之asynccontextmanager学习
开发语言·python·学习
hqwest6 小时前
码上通QT实战08--导航按钮切换界面
开发语言·qt·slot·信号与槽·connect·signals·emit
AC赳赳老秦7 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
不知道累,只知道类7 小时前
深入理解 Java 虚拟线程 (Project Loom)
java·开发语言
国强_dev8 小时前
Python 的“非直接原因”报错
开发语言·python
YMatrix 官方技术社区8 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix