数组声明 []
数组的数据有下biao
整型数组 int [] timp={ 2, 2, 2};
字符串数组 String timp={ "你", "好"};
Console.WrintrLine();//语句输出
数组声明 []
数组的数据有下biao
整型数组 int [] timp={ 2, 2, 2};
字符串数组 String timp={ "你", "好"};
Console.WrintrLine();//语句输出
bool isRight = true;
string str = Console.ReadLine();
for(int i = 0; i < str.Length; i++)
{
if((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'z') && (str[i] < 'A' || str[i] > 'Z') && (str[i] != '_'))
{
//不合法
isRight = false;
}
else if (str[0] >= '0' && str[0] <= '9')
{
//不合法
isRight = false;
}
}
if (isRight)
{
Console.WriteLine("输入合法");
}
else
{
Console.WriteLine("输入不合法");
}
for (int i = intArray.Length - 1; i > 0; i--)
{
for (int j = 1; j < i + 1; j++)
{
if (intArray[j - 1] > intArray[j])
{
int temp = intArray[j - 1];
intArray[j - 1] = intArray[j];
intArray[j] = temp;
}
}
}
foreach (int i in intArray)
{
Console.Write(i + " ");
}
委托
委托(delegate是一种存储函数引用的类型)
委托的定义是指定了一个返回类型和一个参数类型
定义委托之后,就可以声明该委托类型的变量,接着就可以把一个返回类型跟参数列表跟委托一样的函数赋值给这个变量
委托的使用分两步
1.定义
2.声明
定义委托的时候,跟函数有点像,只不过没有函数体。
只能赋值-参数和返回值一样的函数
delegate double MyDelegate(double XX,double XX)
delegate //定义一个委托
double //返回值为double
使用的话需要返回值类型一致
怎么遍历一个数组?
int[] temp = new int[5] { 5, 6, 7, 8, 9 };
for(int a = 0; a < 5;a++)
{
Console.WriteLine(temp[a]);
}
2. foreach遍历数组:
int[] temp;
temp = new int[] { 5, 56, 7, 89, 23, 46, 459, 2, 77, 85, 65 };
foreach(int i in temp)
{
Console.Write(i + " ");
}
i
声明数组:
数据类型[] 数组名;
初始化数组:
数组名 =new 数据类型[];
初始化的元素类型与声明时的类型必须相同。
例: int[] array01 = new int[5];
string[] array02 = new string[3];
怎么遍历一个数组?
int[] temp = new int[5] { 5, 6, 7, 8, 9 };
for(int a = 0; a < 5;a++)
{
Console.WriteLine(temp[a]);
}
2. foreach遍历数组:
int[] temp;
temp = new int[] { 5, 56, 7, 89, 23, 46, 459, 2, 77, 85, 65 };
foreach(int i in temp)
{
Console.Write(i + " ");
}
int i 为临时变量,随着遍历的进行,i中的数值也是变化的,所以叫做临时变量。
foreach只能正序遍历,如果想倒序遍历数组可以用for或while循环语句来写。
3. 获得位置数组中数值的长度,也就是数值的个数。
int[] ages = {2,3,4,5,6,7,8,9};
Console.Write(ages.Length);
输出结果为:8 这样就可以获得数组中数值的个数了。
例如:
int[] temp;
temp = new int[] { 5, 56, 7, 89, 23, 46, 459, 2, 77, 85, 65 };
Console.WriteLine(temp.Length);
for(int i = 0; i < temp.Length; i++)
{
Console.Write(temp[i] + " ");
}
temp.Length可以直接用,用以表示数组中数值的个数。
1. 字符类型里面只能存放一个字符。
2. 没一个字符通过底层存储时都会转变成01的形式,所以也是整数类型变量。
例如:
char a='a';
intb=a;
char a = "c"; //赋予字符型a值为“c”。
int b = a; //将字符型变量赋值给整数类型b。
问:赋值没有弄明白。字符型'a'不是给了变量b了么,怎么还能出现99呢,它不是字符么?
猜:是不是变量类型不同,所以就会根据变量类型而变化掉了?
3. 强制类型转换
int a = 97; //整数型可以存储上亿的变量。
char b = (char)a; //字符型只可以存储几百,等同于将大个的东西强塞进小容器里,那么这种就是强制塞进去的,一部分数据可能会丢失。
Debug.Log(a);
Debug.Log(a);
输出结果为:
97
a
Random里有Next();方法,可存放两位数字,在这两位数之间生成随机数。
bread; 终止循环
foreach
///分割
string[]strArray=name.split(“”,“”);
foreach(string str in strArray)
{
Debug.Log(str);
}
string name="Micheal";
//把字符串转成小写字母
string str2=name.ToLower();
Debug.Log(name+"-"+str2);
//把字符串转成大写字母
string str3=name.ToUpper();
Debug.Log(name+"-"+str3);
//去掉字符串中的空格
string str4=name.Trim();
Debug.Log(name+"-"+str4);
//去掉字符串前面的空格
string str5=name.TrimStart();
Debug.Log(name+"-"+str5);
//去掉字符串后面的空格
string str6=name.TrimEnd();
Debug.Log(name+"-"+str6);
类型的显示转换与隐式转换
类型的转换得是同一类型
隐式转换:不需要写太多的代码,自动进行
显示转换:需要写代码告诉转成是什么类型
byte a=32;
int b=1000;
b=a;//隐式转换
a=(byte)b;//显示转换
一个项目当中有很多“类”,类里面会有很多方法,运行程序就是运行的类下面的“方法”
Main:是入口方法,一个项目中只能有一个入口方法
C#百科https://baike.com
for循环 适合 确定循环次数
while循环 适合 确定循环条件
do while循环 适合 循环至少被执行一次
符号条件
=新左边是否等于右边
>
判断左边是否大于右边
<
判断左边是否小右边
=判新左边是否大于或等于右边
<=
判断左边是否小于或等于右边
ulong
string shuru = Console.ReadLine();
char letter;
for (int i = 0; i < shuru.Length; i++)
{
int temp = (int)shuru[i];
if((temp >= 65 && temp <= 90) || (temp >= 97 && temp <= 122))
{
if(temp < 88 || (temp >= 97 && temp < 120))
{
temp += 3;
letter = (char)temp;
Console.Write(letter);
}
else if((temp >= 88 && temp <= 90) || temp >= 120)
{
temp -= 23;
letter = (char)temp;
Console.Write(letter);
}
}
else
{
Console.Write(shuru[i]);
}
}
Console.ReadKey();
VS中的字体选择可以选
1.Consolas
2.JetBrains Mono
运算符的优先级