C#学习笔记(九):函数、代码查询和调试

代码查询和调试

代码查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace m1w2d6_debug
{
    struct Point2d
    {
    }
    class Program
    {
        static void Main(string[] args)
        {
            #region 代码查询
            //查询类型
            //右键点击需要查询的类型,选择转到定义
            //将鼠标悬停在对应的方法上
            //可以知道这个方法的返回值和参数,还可以知道他是否有不同的重载
            Point2d p = new Point2d();
            String s = "    字符串                ";
            //类名调用方法 如果定义中 有static关键字 就是用类名调用的方法
            bool isNull = string.IsNullOrEmpty(s);//string 和 String 特殊的系统类型的别称
            Console.WriteLine(isNull ? "是空串" : "不是空串");
            //变量调用方法 如果定义中 没有static关键字 就是用变量调用方法
            Console.WriteLine(s.Length);
            Console.WriteLine(s);
            s = s.Trim();
            Console.WriteLine(s.Length);
            Console.WriteLine(s);
            //看API学习Split(重载)和SubString方法
            //String.Split 方法有6个重载函数:
            string str = "attack,defend,health,speed";
            char[] chars = { '0' };
            //1、用字符串分隔:            
            string str1 = "aaajsbbbjsccc";
            string[] sArray1 = Regex.Split(str1, "js", RegexOptions.IgnoreCase);
            foreach (string i in sArray1) Console.Write(i.ToString() + "
");
            Console.WriteLine("
");
            //输出结果:
            //aaa
            //bbb
            //ccc
            //2、用多个字符来分隔:
            string str2 = "aaajbbbscccjdddseee";
            string[] sArray2 = str2.Split(new char[2] { 'j', 's' });
            foreach (string i in sArray2) Console.Write(i.ToString() + "
");
            Console.WriteLine("
");
            //输出结果:
            //aaa
            //bbb
            //ccc
            //ddd
            //eee
            //3、用单个字符来分隔:
            string str3 = "aaajbbbjccc";
            string[] sArray3 = str3.Split('j');
            foreach (string i in sArray3) Console.Write(i.ToString() + "
");
            Console.WriteLine("
");
            //输出结果:
            //aaa
            //bbb
            //ccc
            //SubString 方法:
            //程序代码
            //程序代码
            string str = "abcdefgh";
            Response.Write(str.Substring(0, 1));//return:a
            Response.Write(str.Substring(2, 3));//return:cde
            Response.Write(str.Substring(7, 1));//return:h
            Response.Write(str.Substring(7));//return:h
            Response.Write(str.Substring(10));//error:startIndex 不能大于字符串长度。
            Response.Write(str.Substring(7, 10));//error:索引和长度必须引用该字符串内的位置。
            #endregion
            #region 代码调试
            //
            int count = 0;
            while (true)
            {
                Console.WriteLine(count++);
                Console.WriteLine("我要学好C#");
            }
            #endregion
        }
    }
}

代码调试

打开调试窗口

函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w2d6_function_flyingchess
{
    class Program
    {
        //比较两个数字的大小,返回最大值。
        //对 两个数字 做 比大小 ,要最大的那个
        static int GetMax(int a, int b)//要在主函数中使用,主函数是static,所以要加static
        {
            return a > b ? a : b;
        }
        //写一个方法,判定你传入的参数是不是质数。
        //对 对谁(参数) 做什么(函数体) 结果如何(返回类型)
        //对 一个数 判定是否是质数 告诉我是或者不是(打印,返回)
        static void GetNum(int num)
        {
            for (int i = 2; i < num; i++)
            {
                if (num%i==0)
                {
                    Console.WriteLine("不是质数");
                }
            }
            Console.WriteLine("是质数");
        }
        static void GetNum1(int num)
        {
            for (int i = 2; i < num; i++)
            {
                if (num % i == 0)
                {
                    Console.WriteLine("{0}不是质数", i);
                }
                Console.WriteLine("{0}是质数", i);
            }
        }
        //圆的周长和面积
        static float[] GetCircle(int r)
        {
            float PI = 3.1415926f;
            float area = r * r * PI;
            float perimeter = 2 * r * PI;
            float[] array = new float[2] { area, perimeter };
            return (array);
        }
        //数组计算
        static void GetArray(int[] array)
        {
            float sum = 0f;
            int max = 0;
            int min = 0;
            float average = 0f;
            for (int i = 0; i < array.Length - 1; i++)
            {
                sum += array[i];
                if (array[i] > max)
                {
                    max = array[i];
                }
                else if (array[i] < min)
                {
                    min = array[i];
                }
                average = sum / array.Length - 1;
            }
            Console.WriteLine("总和是{0}", sum);
            Console.WriteLine("最大是{0}", max);
            Console.WriteLine("最小是{0}", min);
            Console.WriteLine("平均是{0}", average);
        }
        //数组排序
        static int[] GetArrayRank(int[] array)
        {
            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = 0; j < array.Length - 1 - i; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
            return array;
        }
        //判断闰年
        static void GetYear(int year)
        {
            while (true)
            {
                if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
                {
                    Console.WriteLine("是闰年");break;
                }
            }          
        }
        static void Main(string[] args)
        {
            //函数是对逻辑(语句序列)的封装,方便以后重复使用
            //函数的签名{函数体}
            //指令逻辑(什么指令) 对谁(参数) 做什么(函数体) 结果如何(返回类型)
            //参数 可以是任意类型
            //函数体 可以是任意语句
            //返回类型 可以是任意类型 void(无返回类型)
            //如果指定了返回类型 必须有相应的返回值
            //使用return可以返回一个值,并结束函数
            //如果你使用了void,也可以使用return,这时,他不再返回值,但结束函数
            //返回类型 函数名 (参数列表)
            //{
            //   函数体
            //}
            int max = GetMax(3, 5);//函数如果有返回类型,你可以直接使用函数返回值
            Console.WriteLine(max);
            //void无返回值
            GetNum(37);
            GetNum1(37);
            //圆的周长和面积
            float[] sArray1 = new float[2];
            sArray1 = GetCircle(4);
            foreach (float i in sArray1) Console.Write(i.ToString() + "
");
            //数组计算
            int[] sArray2 = new int[4] { 6, 7, 8, 9 };
            GetArray(sArray2);
            //数组排序
            int[] sArray3 = new int[] { 10, 9, 7, 5, 3, 1, 2, 4, 8, 6 };          
            foreach (float i in sArray3) Console.Write(i.ToString() + "	");
            Console.WriteLine("
");
            sArray3 = GetArrayRank(sArray3);
            foreach (float i in sArray3) Console.Write(i.ToString() + "	");
            Console.WriteLine("
");
            //判断闰年
            int year = int.Parse(Console.ReadLine());
            GetYear(year);
          
        }
    }
}
原文地址:https://www.cnblogs.com/vuciao/p/10362380.html