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;
        }
    }
}
相关推荐
Ray Liang2 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi5 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端