下面我們繼續學習C#的語法,結構struct,C#中的結構和我們PLC中建立的UDT(結構體)是一樣的,里面存盤了相關的不同型別的資料,
有一句話我覺得十分重要:方法是依存于結構和物件存在的,這以后我們會個更加深入的學習的,
Struct結構:
可以幫助我們一次性宣告不同型別的變數,
語法:
[public] struct 結構名
{
成員;
}
如下例宣告:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 public struct Person 12 { 13 public string name; 14 public int age; 15 public char gender; 16 } 17 static void Main(string[] args) 18 { 19 Person zsPerson; 20 zsPerson.name = "張三"; 21 zsPerson.age = 18; 22 zsPerson.gender = '男'; 23 24 Console.ReadKey(); 25 } 26 } 27 }
值得我們注意的是,在宣告結構的時候,如果我們沒加public,我們是建立不了給結構賦值的,不加public系統默認為private私有的,并且我們在命名空間之下Main之上創建的變數其實不叫變數,而是叫欄位,
變數和欄位的區別在于:變數可以存一個值,之后不停被覆寫,而欄位類似我們PLC背景資料,可以存盤若干個數值,
而且我在這要提出一個問題,我看了幾個視頻和資料,對于欄位的命名說法不一樣的,總結如下
(1)欄位和變數要區別命名,例如:_Name
(2)也有反對這種命名方式的,理由是:在復雜的編程任務中,可能影響與其他語言的互動參考的作用,例如VB,net,
這在以后深入學習程序中我們在慢慢體會,也歡迎大神們給我解惑,
陣列
一次性存盤多個相同型別的變數,
語法:
陣列的型別[] 陣列名 = new 陣列型別[陣列長度];
陣列的長度一旦固定了,就不能在被改變了,
對于int[]型別的陣列,初值為0,string[]陣列初值為null,bool[]陣列初值為false,
下面我們介紹幾種宣告陣列的方式
int[] nums = new int[10]; //沒有宣告陣列元素,推薦
int[] nums = {1,2,3,4,5,6}; //隱式宣告了元素和長度,推薦
int[] nums = new int[3]{1,2,3}; //不推薦,麻煩且長度和元素數量必須一致,
int[] nums = new int[]{1,2,3,4,5}; //類似第2種
下面看一個練習1:從一個整數陣列中求出最大值,最小值,總和和平均值,
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,2,3,4,5,6,7,8,9,0}; 14 int max = nums[0]; 15 int min = nums[0]; 16 int sum = 0; 17 18 for (int i = 0; i < nums.Length; i++) 19 { 20 if (nums[i] > max) 21 { 22 max = nums[i]; 23 } 24 25 if (nums[i] < min) 26 { 27 min = nums[i]; 28 } 29 sum += nums[i]; 30 } 31 Console.WriteLine($"這個陣列的最大值是{max},最小值是{min},總和是{sum},平均值是{sum/nums.Length}"); 32 Console.ReadKey(); 33 } 34 } 35 }
練習2:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "老楊","老蘇","老鄒","老虎","老牛","老馬"}; 14 string str = null; 15 16 for (int i = 0; i < names.Length-1; i++) 17 { 18 str += names[i] + "|"; 19 } 20 Console.WriteLine(str+names[names.Length-1]); 21 Console.ReadKey(); 22 } 23 } 24 }
練習3:對一個整數陣列做如下處理:若元素為正數將這個元素+1,若為負數,將這個元素-1,元素為0,不變,
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,-2,3,-4,5,6,0}; 14 for (int i = 0; i < nums.Length; i++) 15 { 16 if (nums[i] > 0) 17 { 18 nums[i] += 1; 19 } 20 else if (nums[i] < 0) 21 { 22 nums[i] -= 1; 23 } 24 else 25 { 26 27 } 28 } 29 30 for (int i = 0; i < nums.Length; i++) 31 { 32 Console.WriteLine(nums[i]); 33 } 34 Console.ReadKey(); 35 } 36 } 37 }
練習4:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "我","是","好人"}; 14 for (int i = 0; i < names.Length/2; i++) 15 { 16 string temp = names[i]; 17 names[i] = names[names.Length - 1 - i]; 18 names[names.Length - 1 - i] = temp; 19 } 20 for (int i = 0; i < names.Length; i++) 21 { 22 Console.Write(names[i]); 23 } 24 Console.ReadKey(); 25 } 26 } 27 }
練習5:冒泡排序:就是將一個陣列中的元素從大到小,從小到大排列,
分析:需要兩個回圈,外層回圈,控制比較次數,內層回圈,控制交換次數,
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 9,8,7,6,5,4,3,2,1,0}; 14 for (int i = 0; i < nums.Length-1; i++) 15 { 16 for (int j = 0; j < nums.Length-1-i; j++) 17 { 18 if (nums[j] > nums[j+1]) 19 { 20 int temp = nums[j]; 21 nums[j] = nums[j + 1]; 22 nums[j + 1] = temp; 23 } 24 } 25 } 26 for (int i = 0; i < nums.Length; i++) 27 { 28 Console.WriteLine(nums[i]); 29 } 30 Console.ReadKey(); 31 } 32 } 33 }
這里面有一點值得我們注意,C#中的陣列下標和我們PLC中陣列下標正好相反,C#中陣列下標的0從左面元素開始計算,
其實,這種冒泡方式的寫法也就在面試的時候會用到,在我們C#中,可以直接用一個方法解決Array.Sort();(只能升序)
Array.Reverse();(反轉排列)若想降序:先呼叫Array.Sort();后呼叫Array.Reverse(),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/86437.html
標籤:C#
