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;
        }
    }
}
相关推荐
艾莉丝努力练剑27 分钟前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
胡萝卜3.042 分钟前
深入理解string底层:手写高效字符串类
开发语言·c++·学习·学习笔记·string类·string模拟实现
西柚小萌新1 小时前
【Python从入门到精通】--Pycharm增加内存
开发语言·python·pycharm
不爱编程的小九九1 小时前
小九源码-springboot082-java旅游攻略平台
java·开发语言·旅游
只是懒得想了1 小时前
用C++实现一个高效可扩展的行为树(Behavior Tree)框架
java·开发语言·c++·design-patterns
yan8626592461 小时前
于 C++ 的虚函数多态 和 模板方法模式 的结合
java·开发语言·算法
Small___ming1 小时前
【Python基础】Python路径操作全解析:os.path、glob与pathlib从入门到精通
开发语言·python
云草桑2 小时前
.net AI MCP 入门 适用于模型上下文协议的 C# SDK 简介(MCP)
ai·c#·.net·mcp
_poplar_2 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
lly2024062 小时前
Ruby Socket 编程
开发语言