日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 小世界的野孩子 2021-04-10

具體要求為:

使用一個二維數組記錄客車售票系統(tǒng)中的所有座位號,并在每個座位號上都顯示有票,然后用戶輸入一個坐標位置,按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);

 

先創(chuàng)建如下腳本,然后在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     }

 

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多