假設客車的座位數是9行4列,使用二維數組在控制台應用程序中實現簡單的客車售票系統。


具體要求為:

使用一個二維數組記錄客車售票系統中的所有座位號,並在每個座位號上都顯示有票,然后用戶輸入一個坐標位置,按Enter鍵,即可將該座位號顯示為已售。

首先我定義的輸入格式為:1,2

個人認為主要知識點偽代碼如下

1.字符串分割

char[] separator = { ',' }; 

  splitstrings = str.Split(separator);

2.字符串前后去空

str.Trim()

3.轉換類型,如果不是int類型則為false,可以處理異常情況。

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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM