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;
        }
    }
}
相关推荐
NEU-UUN10 小时前
C语言 . 第三章第三节 . 变参函数
c语言·开发语言
hnxaoli10 小时前
win10程序(十四)pdf转docx简易版
开发语言·python·pdf
CodeCraft Studio10 小时前
PDF处理控件Aspose.PDF教程:在Python中向PDF文档添加页面
开发语言·python·pdf
ftpeak10 小时前
《Rust+Slint:跨平台GUI应用》第五章 基础元素
开发语言·ui·rust·slint
寻找华年的锦瑟10 小时前
Qt Quick Application&&Qt Quick Application (compat)
开发语言·qt
国服第二切图仔10 小时前
Rust开发实战之WebSocket通信实现(tokio-tungstenite)
开发语言·websocket·rust
echoyu.10 小时前
java源代码、字节码、jvm、jit、aot的关系
java·开发语言·jvm·八股
唐青枫11 小时前
C#.NET SemaphoreSlim 深入解析:轻量级异步锁与并发控制
c#·.net
麦麦大数据11 小时前
MacOS 安装Python 3.13【同时保留旧版本】
开发语言·python·macos·python安装
上去我就QWER13 小时前
Qt中如何获取系统版本信息
开发语言·qt