一.命名规则
1.方法命名是用帕斯卡
2.变量使用驼峰命名法
3.is开头的都是bool I开头的都是接口 abs开头的是抽象类
4.变量初始要赋值
二.字符串
1.char.IsDigit: 判断该char是否是10进制数值
class Program
{
/// <summary>
/// 数字返回True不是返回false
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static bool isNumber(string str)
{
bool returnValue = true;
//循环用户输入的每一个字符
for (int i = 0; i < str.Length; i++)
{
//int.TryParse();
//判断字符是不是十进制数字
if (!char.IsDigit(str[i]))
{
//若不是返回false
returnValue = false;
break;
}
}
return returnValue;
}
static int isNum(string str)
{
int val = 0;
//转换成功 tryparse返回true val 返回数值
//转换失败 tryparse返回false val 返回0
if (!int.TryParse(str,out val))
{
return val;
}
return val;
}
static void Main(string[] args)
{
int b = isNum("132a6");
Console.WriteLine(b);
Console.ReadKey();
}
}
2.字符串查找
IndexOf LastIndexOf
static void Main(string[] args)
{
string str = "C#编程我喜欢,C#编程我呵呵哒C#";
// int tmpIndex = str.IndexOf("C#");
int tmpIndex = str.LastIndexOf("C# ");
Console.WriteLine(tmpIndex);
}
3.字符串比较
Equals Compare
string str01 = "ABC";
string str02 = "ABc";
// Console.WriteLine(str02.Equals(str02));
//返回为0 代表 01 == 02 //true 代表忽略大小写
int tmpInt = string.Compare(str01, str02, true);
Console.WriteLine(tmpInt);
4.字符串截取
SubString
string str = "你好世界,呵呵哒";
//从第0位开始 截取3个字符
//string newStr = str.Substring(0, 3);
//截取第五位开始 截取后面的全部
string newStr = str.Substring(5);
Console.WriteLine(newStr);
5.字符串替换
Replace
string str = "呵呵哒,呵呵哒你好世界,呵呵哒";
//全部替换
string newStr = str.Replace("呵呵哒", "C# 编程");
Console.WriteLine(newStr);
6.字符串分割
Split(‘符号’)
7.空字符串
string str01 = ""; // 空值
string str02 = string.Empty; // 空值
string str03; // 空对象
三
1.
out: 传出参数,在方法内部必须赋值
ref: 传入参数,在方法外部必须赋值
2.
OOP编程
对象是一个抽象概念, 用代码表示就是new出来的基本都是对象
在unity中最能体现OOP三大特性中的哪一个?
3.
命名空间就是包含功能类似的class的一个集合
public: 对象和子类都能访问
protected:子类可以访问,子类对象不能访问
private: 对象和子类都不能访问
base: 就是访问父类成员(公开和保护的)
四
1.随机产生二进制 并转化为10进制输出
class Program
{
/// <summary>
/// 求次方
/// </summary>
/// <param name="number">那个数的次方</param>
/// <param name="cifang">几次方</param>
/// <returns>number^cifang</returns>
static int CiFang(int number, int cifang)
{
int returnValue = 0; // 返回值
if (number == 0) // 0的次方都是0
{
return returnValue;
}
if (cifang == 0) // 任何数的0次方都是1
{
returnValue = 1;
return returnValue;
}
if (cifang == 1) // 任何数的1次方都是任何数
{
returnValue = number;
return returnValue;
}
returnValue = number;
if (number > 0)
{
for (int i = 1; i < cifang; i++)
{
//次方
returnValue *= number;
}
//返回最终结果
return returnValue;
}
else
{
//负数先转正数计算值
number = -number;
returnValue = number;
for (int i = 1; i < cifang; i++)
{
returnValue *= number;
}
//判断有无负号
if (cifang%2==0)
return returnValue;
else
{
return -returnValue;
}
}
}
static void RandomNumber()
{
int[] randomInts = new int[5];
Random r = new Random();
for (int i = 0; i < randomInts.Length; i++)
{
randomInts[i] = r.Next(0, 2);
}
//遍历
foreach (int i in randomInts)
{
Console.Write(i);
}
Console.WriteLine();
Console.WriteLine("====================");
//弄一个10个数值 0~1
int number = 0;
//使用按位展开法逆向得出10进制
for (int i = 0; i < randomInts.Length; i++)
{
//1. 获取一个二进制位
number += CiFang((randomInts[i] == 0) ? 0 : 2, randomInts.Length - 1 - i);
// 2. 得知是N次方
}
Console.WriteLine(number);
}
static void Main(string[] args)
{
RandomNumber();
Console.WriteLine(CiFang(-2, 1));
Console.ReadKey();
}
}
2.unity中类似跑马灯效果
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColorByTime : MonoBehaviour
{
// Use this for initialization
//定义两个数组
private GameObject[] _cubes;
private Color[] _colors;
void Start()
{
//初始化
_colors = new Color[] { Color.black, Color.blue, Color.green, Color.red, Color.yellow, Color.white };
//寻找物体
_cubes = GameObject.FindGameObjectsWithTag("MyCube");
//协程开始
StartCoroutine("changeColorEnumerator");
}
// Update is called once per frame
void Update()
{
//协程关闭判断
if (Input.GetKeyDown(KeyCode.Q))
{
StopCoroutine("changeColorEnumerator");
}
}
IEnumerator changeColorEnumerator()
{
int j = 0;
//协程
while (true)
{
//i==0
for (int i = 0; i < _colors.Length; i++)
{
//给对应物体颜色 //使之物体颜色 每次偏移1 且可以循环
_cubes[i].GetComponent<MeshRenderer>().material.color = _colors[(i + j) % 6];
}
//等待三秒
yield return new WaitForSeconds(3);
j++;
//控制 int最大值 -2147483648~2147483647
if (j >= int.MaxValue - 7)
{
j = 0;
}
}
}
}