C#笔记 -4

枚举

using System;

namespace _枚举
{
    enum Gamestate
    {
        Pause,
        Faild,
        Success,
        Start
    }//枚举放在namespace下和class都可以
    class learn
    {
        static void Main(string[] args)
        {
            Gamestate one = Gamestate.Start;
            Console.WriteLine(one);
            if (one == Gamestate.Start)
            {
                Console.WriteLine("游戏运行中");
            }
            int a = (int)one;//枚举类型转换成int类型由大到小,需要显示转换
            switch (a)
            {
                case 0:
                    Console.WriteLine("Pause");
                    break;
                case 1:
                    Console.WriteLine("Faild");
                    break;
                case 2:
                    Console.WriteLine("Success");
                    break;
                case 3:
                    Console.WriteLine("Start");
                    break;
            }
        }
    }
}

结构体

和c语言一致

using System;

namespace 结构体
{
    struct Postion
    {
        public float x;
        public float y;
        public float z;
    }
    class Learn
    {

        static void Main(string[] args)
        {
            Postion weak1;
            weak1.x = 12.3f;
            weak1.y = 11.1f;
            weak1.z = 23.42f;
            Console.WriteLine(weak1.x);
        }

    }
}

数组

C#的数组具有java数组的特定

声明数组
int [] a;
和c语言不同,方括号在数组名前面。
二维数组
int [,] a;
C#中用[,]的格式来表示几行几列,比如[1,2]一行两列

两种声明方法

声明时赋值


int [] a ={1,2,34,5,6,3};

new


int [] a = new int[10];//这里可以定义数组长度
当然,也可以在声明后初始化
int[] a;
a = new int[10];

结合一二种方式

int[] a = new int[3]{1,2,3};

using System;

int[] a1 = { 1, 2, 3 };
int[] a2;
a2 = new int[3];

int[] a3 = new int[3] { 1, 2, 3 };

for (int i = 0; i < a3.GetLength(0); i++)
{
    Console.WriteLine(a3[i]);
}
Console.WriteLine("输入三个数");
for (int i = 0; i < 3; i++)
{
    a3[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < a3.GetLength(0); i++)
{
    Console.WriteLine(a3[i]);
}

数组的遍历

循环遍历for while

foreach遍历

using System;

int[] a1 = { 1, 2, 3 };
int[] a2;
a2 = new int[3];

int[] a3 = new int[3] { 1, 2, 3 };

for (int i = 0; i < a3.GetLength(0); i++)
{
    Console.WriteLine(a3[i]);
}
Console.WriteLine("输入三个数");
for (int i = 0; i < 3; i++)
{
    a3[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < a3.GetLength(0); i++)
{
    Console.WriteLine(a3[i]);
}

使用.Length获取数组元素个数

字符串的处理

通过循环遍历来处理字符串

using System;

string str = "www.touming.xyz";
for (int i = 0; i < str.Length; i++)
{
    Console.Write(str[i]);
    Console.Write(" ");
}

可以看到,str在获取字符的时候,其本身状态其实是一个字符数组,可以通过数组访问。

ToLower方法

将字符串转换成小写形式的副本。

ToUpper方法

将字符串转换成大写形式的副本。

Trim方法

去掉字符串前后的空白字符

Split方法

把原字符串按照指定的字符进行拆分,得到一个拆分后的字符串数组

using System;

string www = " www .touming.XYZ ";
string ret_1 = www.ToLower();
Console.WriteLine(www);
Console.WriteLine(ret_1);
string ret_2 = www.ToUpper();
Console.WriteLine(ret_2);
string ret_3 = www.Trim();
Console.WriteLine(ret_3 + "|");

string[] ret_4 = (ret_3).Split('.');
foreach (string temp in ret_4)
{
    Console.WriteLine(temp);
}

函数

函数的定义和使用

using System;

namespace 函数的定义和使用
{
    class Program
    {
        static int Add(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(Add(a, b));
        }
    }
}

参数数组

using System;

namespace 参数数组
{
    class Program
    {
        static int Add(int[] arr, int sz)
        {
            int add = 0;
            for (int i = 0; i < sz; i++)
            {
                add += arr[i];
            }
            return add;

        }
        static void Main(string[] args)
        {
            int[] infoNum = new int[100];

            int i = 0;
            int sz = 1;
            Console.WriteLine("输入数字,回车输入下一个,#截至");
            while (true)
            {
                string ret = Console.ReadLine();
                if (ret == "#")
                {
                    break;
                }
                try
                {
                    infoNum[i] = Convert.ToInt32(ret);
                }
                catch (Exception e)
                {
                    Console.WriteLine("存在非数字的字符");
                    continue;
                }
                i++;
                sz++;
            }
            int retNum = Add(infoNum, sz);
            Console.WriteLine("和为" + retNum);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值