具體要求爲:數組
使用一個二維數組記錄客車售票系統中的全部座位號,並在每一個座位號上都顯示有票,而後用戶輸入一個座標位置,按Enter鍵,便可將該座位號顯示爲已售。函數
首先我定義的輸入格式爲:1,2ui
我的認爲主要知識點僞代碼以下spa
1.字符串分割code
char[] separator = { ',' }; blog
splitstrings = str.Split(separator);字符串
2.字符串先後去空string
str.Trim()it
3.轉換類型,若是不是int類型則爲false,能夠處理異常狀況。class
int columnNum = 0;
bool isColumn = int.TryParse(column, out columnNum);
先建立以下腳本,而後在Main函數中直接調用便可。
1 public class TicketingSystem 2 { 3 int[,] seatCount = new int[9, 4]; 4 5 public void CheckTicketCount() 6 { 7 bool res = true; 8 String[] splitstrings = { "row", "col"}; 9 char[] separator = { ',' }; 10 while (res) 11 { 12 Console.WriteLine("請輸入座位號:"); 13 string str = Console.ReadLine(); 14 splitstrings = str.Split(separator); 15 if (str.Trim() == "Quit") 16 { 17 res = false; 18 Console.WriteLine("結束購票"); 19 return; 20 } 21 22 if (splitstrings.Length < 2) 23 { 24 Console.WriteLine("輸入的格式不正確"); 25 continue; 26 } 27 string row = splitstrings[0].Trim(); 28 string column = splitstrings[1].Trim(); 29 30 int rowNum = 0; 31 bool isRow = int.TryParse(row, out rowNum); 32 if (!isRow || rowNum >= seatCount.GetLength(0)) 33 { 34 Console.WriteLine("輸入的行不正確"); 35 continue; 36 } 37 38 int columnNum = 0; 39 bool isColumn = int.TryParse(column, out columnNum); 40 if (!isColumn || columnNum >= seatCount.GetLength(1)) 41 { 42 Console.WriteLine("輸入的列不正確"); 43 continue; 44 } 45 if (seatCount[rowNum, columnNum] == 1) 46 { 47 Console.WriteLine("該座位已經被購買!"); 48 continue; 49 } 50 seatCount[rowNum, columnNum] = 1; 51 Console.WriteLine(rowNum + "行" + columnNum + "列車票售出"); 52 bool isEmptySeat = false; 53 for (int i = 0; i < seatCount.GetLength(0); i++) 54 { 55 for (int j = 0; j < seatCount.GetLength(1); j++) 56 { 57 if (seatCount[i, j] == 0) 58 { 59 isEmptySeat = true; 60 break; 61 } 62 } 63 if (isEmptySeat) 64 { 65 break; 66 } 67 } 68 69 if (!isEmptySeat) 70 { 71 res = false; 72 Console.WriteLine("車票售完!"); 73 return; 74 } 75 Console.WriteLine(); 76 Console.WriteLine(); 77 } 78 } 79 }