#region MyDefinedFunction ////// MyDefinedFunction /// public void MyDefinedFunction() { // for循环 for (int i = 0; i < 5; i++) { } // while循环 int j = 0; while (j < 3) { j++; } // do... while循环 int k = 0; do { k++; } while (k < 3); //定义变量遵循驼峰命名法 //驼峰命名法:多个单词组合的变量,首单词全小写,后续所有单词的首字母大写,其余小写 // 数组最大的索引值为数组长度-1 // 数组索引从0开始 //方法一:定义int类型的数组 // 声明没有元素的数组,需要指定数组的长度 int[] myIntArray1 = new int[4]; myIntArray1[0] = 2; myIntArray1[1] = 4; myIntArray1[2] = 6; myIntArray1[3] = 8; //方法二:定义int类型的数组 //声明初始化有元素的数组 int[] myIntArray2 = new int[] { 2, 4, 6, 8 }; //找到数组索引为0的值 int returnedResult = myIntArray2[0]; // int类型显示转换 double doubleDigit = 3.12; int intDigit = (int)doubleDigit; intDigit = int.Parse(doubleDigit.ToString()); intDigit = Convert.ToInt32(doubleDigit); //精度有损耗 intDigit = int.Parse(null); //报错,无法转换 intDigit = Convert.ToInt32(null); //空对象默认赋值为0 //在班级中找到赵六同学 //方法一:定义string类型的数组 string[] ourClass1 = new string[6]; ourClass1[0] = "张三"; ourClass1[1] = "李四"; ourClass1[2] = "王五"; ourClass1[3] = "赵六"; ourClass1[4] = "田七"; ourClass1[5] = "周八"; //方法二:定义string类型的数组 string[] ourClass2 = new string[] { "张三", "李四", "王五", "赵六", "田七", "周八" }; //方法三:定义string类型的数组(简写) //string[] ourClass2 = { "张三", "李四", "王五", "赵六", "田七", "周八" }; //solution 1 for (int i = 0; i < ourClass2.Length; i++) { if (ourClass2[i] == "赵六") { // show "find it !" break; } } int counter1 = 0; //solution 2 while (counter1 < ourClass2.Length) { if (ourClass2[counter1] == "赵六") { // show "find it !" break; } counter1++; } //solution 3 string studentName = ""; bool isFind = true; counter1 = 0; while (studentName != "赵六") { if (counter1 >= ourClass2.Length) { // show "cannot find it !" isFind = false; break; } studentName = ourClass2[counter1++]; } if (isFind) { // show "find it !" } //solution 4 int counter2 = 0; do { if (ourClass2[counter2] == "赵六") { // show "find it !" break; } counter2++; } while (counter2 < ourClass2.Length); } #endregion



