转战C#---day3
创始人
2024-06-01 13:14:09
0
  • C# 函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static bool VerifyUsername(string username,int num){Console.WriteLine("用户名:"+ username);Console.WriteLine("密码:"+num);return true;}static int Add(int x,int y){return x + y;//return的作用相似于break 后面的语句不再执行。Console.WriteLine("123");} static void Main(string[] args){bool result = VerifyUsername("yyx",12345);int intValue = Add(3,5);Console.WriteLine(result);Console.WriteLine(intValue);Console.ReadKey();}}
}
  • 真素数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static bool IsPrime(int n){int j;if (n <= 1) return false;if (n == 2) return true;for (j = 2; j <= (int)(Math.Pow(n, 1.0 / 2)); j++)if (n % j == 0)return false;return true;}static void Main(string[] args){string str = Console.ReadLine();string[] strArray = str.Split(' ');int[] intArray = new int[strArray.Length];for(int j = 0; j < strArray.Length; j++){int number = Convert.ToInt32(strArray[j]);intArray[j] = number;}int m = intArray[0];int n = intArray[1];for(int i = m; i < n + 1; i++){int number = 0;int temp = i;while (temp % 10 != 0){number = number * 10 + temp % 10;temp /= 10;}if (IsPrime(i) && IsPrime(number)){Console.WriteLine(i);}}Console.ReadKey();}}
}
  • 数组参数和参数数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int Add(int a,int b){return a + b;}static int Add2(int[] array)//数组参数{int sum = 0;foreach(int temp in array){sum += temp;}return sum;}static int Add3(string name,params int[] array)//params代表是参数数组(必须是形参表中的最后一个参数),可以传递任意个数的参数{int sum = 0;foreach (int temp in array){sum += temp;}Console.Write(name+" ");return sum;}static void Main(string[] args){int sum1 = Add2(new int[] { 2, 3, 4 });Console.WriteLine(sum1);int[] array2 = { 6, 8, 9 };int sum2 = Add2(array2);Console.WriteLine(sum2);int sum3 = Add3("xiao ai",6,8,1,3);//会自动将这四个数的组装成数组,作为一个数组参数传参。Console.WriteLine(sum3);}}
}
  • 计算因子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int[] GetIn(int number){int count = 0;for(int i = 1; i < number + 1; i++){if (number % i == 0){count++;}}int[] array = new int[count];//对应因子个数长度的数组int index = 0;for(int i = 1; i < number + 1; i++){if (number % i == 0){array[index] = i;index++;}}return array;}static void Main(string[] args){int number = Convert.ToInt32(Console.ReadLine());int[] array = GetIn(number);foreach(int temp in array){Console.Write(temp + " ");}}}
}
  • 函数的重载:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int MaxValue(int[]array){int max = array[0];for(int i = 1; i < array.Length; i++){if (max < array[i]){max = array[i];}}return max;}static double MaxValue(double [] array){double max = array[0];for (int i = 1; i < array.Length; i++){if (max < array[i]){max = array[i];}}return max;}static void Main(string[] args){Console.WriteLine(MaxValue(new int[] {21,213,123,234,234,23545,433,3123}));Console.WriteLine(MaxValue(new double[] { 21, 213, 123, 234, 234, 23545.9, 433, 312.3 }));}}
}
  • 斐波那契数列(递归):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int fib(int n){if (n == 1 || n == 2)return 1;elsereturn fib(n - 1) + fib(n - 2);}static void Main(string[] args){Console.WriteLine(fib(40));}}
}
  • 阶乘和(递归):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int Factorial(int n){if (n == 1 )return 1;return n * Factorial(n - 1);}static int Add(int n){if (n == 1)return 1;return Add(n-1)+ Factorial(n);}static void Main(string[] args){Console.WriteLine(Add(10));}}
}
  • 1*1+2*2+3*3+...+k*k<2000,求出满足此关系式的k的最大值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static int Pow(int n){if (n == 1 )return 1;return n * n;}static int Add(int n){if (n == 1)return 1;return Add(n-1)+ Pow(n);}static int powAdd(int n){if (n == 1)return 1;return powAdd(n - 1) + n*n;}static void Main(string[] args){int k = 1;int result = 0;while (true){if (powAdd(k) >= 2000){break;}k++;}Console.WriteLine(k - 1);}}
}
  • 枚举类型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{enum RoleType//枚举类型的名字{Mage,Archer,Assassin,Tank,Support,Warrior //枚举类型的值}static void Main(string[] args){RoleType roleType = RoleType.Tank;if(roleType == RoleType.Archer){}}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static void Main(string[] args){GameState gameState = GameState.Menu;gameState = GameState.Running;if(gameState == GameState.Menu){}Console.WriteLine(gameState);}enum GameState{Menu, Running, Pause, Fail, Success}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{class Program{static void Main(string[] args){Week day = Week.Mon;day = Week.Thu;}enum Week{Sun,Mon,Tue,Wed,Thu,Fri,Sat}}
}

虽然声明了新的数据类型,但枚举里的数据本质上依旧是按整数类型来存储的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{   class Program{        static void Main(string[] args){Week day1,day2,day3= Week.Mon;day1 = Week.Sun;int number1 = (int)day1;Console.WriteLine(day1);Console.WriteLine(number1);day2 = Week.Thu;int number2 = (int)day2;Console.WriteLine(day2);Console.WriteLine(number2);day3 = Week.Sat;int number3 = (int)day3;Console.WriteLine(day3);Console.WriteLine(number3);}enum Week{Sun,Mon,Tue,Wed=14,Thu,Fri=100,Sat}}
}
  • 结构体:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace test
{struct StudentInfo{public int age;public string name;public int grade;public int id;}class Program{        static void Main(string[] args){StudentInfo student1;student1.age = 11;student1.name = "xiao ai";student1.grade = 98;student1.id = 01321;Console.WriteLine(student1.name);StudentInfo[] students = new StudentInfo[10];students[0].age = 30;Console.WriteLine(students[0].age);}}
}
  • 结构体函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test2
{class Program{struct Position{public double x;public double y;public double z;public void PrintPostion(){Console.WriteLine(x + "," + y + "," + z);}}static void PrintPostion(Position p){Console.WriteLine(p.x + "," + p.y + "," + p.z);}static void Main(string[] args){Position p1,p2;p1.x = 23.3;p1.y = 3.4;p1.z = 1;// PrintPostion(p1);p1.PrintPostion();p2.x = 1;p2.y = 2;p2.z = 3;p2.PrintPostion();}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test2
{class Program{struct Vector{public double x;public double y;public double z;public double Distance(){double temp = x * x + y * y + z * z;return Math.Sqrt(temp);}}static void Main(string[] args){Vector v1;v1.x = 4;v1.y = 5;v1.z = 7;Console.WriteLine(v1.Distance());}}
}
  • 委托:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test2
{class Program{static double Multiply(double param1, double param2){return param1 * param2;}static double Divide(double param1, double param2){return param1 / param2;}static void Test(){Console.WriteLine("Test");}delegate double MyDelegate(double param1, double param2);//这里的委托相当于没有函数体的函数。delegate void MyDelegate2();static void Main(string[] args){MyDelegate delegate1, delegate2;delegate1 = Multiply;delegate2 = Divide;Console.WriteLine(delegate1(2, 4));Console.WriteLine(delegate2(4, 2));MyDelegate2 delegate3;delegate3 = Test;delegate3();}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test2
{class Program{delegate void OnDieDelegate();static void Play(OnDieDelegate onDie)//这里将委托当作函数的参数来设置{Console.WriteLine("做任务");Console.WriteLine("玩家正在战斗");Console.WriteLine("死亡");if (onDie != null){onDie();}}static void ShowDieUI(){Console.WriteLine("显示游戏死亡后的UI");Console.WriteLine("返回首页UI");}static void Main(string[] args){Play(ShowDieUI);Console.WriteLine();Play(null);}}
}
  • C# 结构体和类:
    结构体:
    没有默认的构造函数,但是可以添加构造函数
    没有析构函数
    没有 abstract 和 sealed(因为不能继承)
    不能有protected 修饰符
    可以不使用new 初始化
    在结构中初始化实例字段是错误的
    类:
    有默认的构造函数
    有析构函数
    可以使用 abstract 和 sealed
    有protected 修饰符
    必须使用new 初始化
    如何选择结构还是类:
    1)堆栈的空间有限,对于大量的逻辑的对象,创建类要比创建结构好一些。
    2)结构表示如点、矩形和颜色这样的轻量对象,例如,如果声明一个含有 1000 个点对象的数组,则将为引用每个对象分配附加的内存。
    在此情况下,结构的成本较低。
    3)在表现抽象和多级别的对象层次时,类是最好的选择。
    4)大多数情况下该类型只是一些数据时,结构时最佳的选择。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...