C#基础篇四数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P01Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[2] { 22, 33 };
            double dd = arr[0];
 
 
            ArrTurnBig();
            Console.ReadLine();
        }
 
        #region 3.0 为数组“扩容”
        /// <summary>
        /// 3.0 为数组“扩容”
        /// </summary>
        static void ArrTurnBig()
        {
            int[] arr = new int[3] { 110, 220, 3330 };
            //arr.Length
            //arr[0] = 11;
 
            //创建一个新的数组,长度(容量)是原来数组的长度 +1(简而言之就是 多一个空间!)
            int[] arrNew = new int[arr.Length + 1];
 
            //1.将 原来数组 里的元素 拷贝 到 新数组 对应下标 的空间中
            //1.1使用代码直接拷贝,缺点:代码量大
            //arrNew[0] = arr[0];
            //arrNew[1] = arr[1];
            //arrNew[2] = arr[2];
            //1.2使用方法拷贝,将 源数组 里的 元素 按照下标 依次拷贝到 目标数组中!
            //   第二个参数是 指 从 目标数组 的 第几个空间开始存放!
            arr.CopyTo(arrNew, 1);
 
           
 
 
        }
        #endregion
 
        #region 2.0 数组的循环 和 初始化器
        /// <summary>
        /// 数组的循环 和 初始化器
        /// </summary>
        static void M12ArrayFor()
        {
            int[] arrint2 = new int[2] { 110, 22 };
            int[] arrInt = new int[3];
            arrInt[0] = 1;
            arrInt[1] = 13;
            arrInt[2] = 12;
            arrInt[0] = 14;
 
            //1.数组的初始化器-----------------------
            //平时我们创建一个数组后,数组里的空间 都是默认值 或者 都是空!
            //                  有默认值的变量:int,float,double... bool(false)
            //                  没有默认值的变量类型:string(null)
            //如果需要给值的话,必须 通过下标 一个个的去给值!太麻烦了~~~~!
            // 初始化器 就是为了能够 方便的 为 数组在创建的 同时 赋值的 语法
            string[] arrStrNames = new string[3] { "小白", "大林老师", "苍老师" };
            string[] arrStrNames2 = new string[] { "小白", "大林老师", "苍老师", "波老师" };
            string[] arrStrNames3 = { "小白", "大林老师", "苍老师", "波老师", "小林老师" };
 
            /*2. 数组专有名词
             *  数组的元素:指的就是 数组里 第n个空间  里存放的值!
             *  数组的长度:数组 的空间个数!
             *  数组的下标:就是指 要访问的数组的空间 的"序号",注意:"序号"从 0 开始!
             */
            //3.数组的遍历------------------------------------
            //3.1一般使用 for循环来遍历数组里的元素
            Console.WriteLine("for 循环 遍历数组:");
            for (int i = 0; i < arrStrNames3.Length; i++)
            {
                string strTemp = arrStrNames3[i];//需要 根据 当前循环的i的数值,去数组中 获取 对应下标的 元素!
                Console.WriteLine(strTemp);
            }
 
            //3.2foreach循环:可以直接 从 数组中 依次 获取 元素
            Console.WriteLine("foreache 循环 遍历数组:");
            foreach (string strElement in arrStrNames3)
            {
                Console.WriteLine(strElement);
            }
        }
        #endregion
 
        #region 1.0 讲解数组基本语法
        /// <summary>
        /// 1.0 讲解数组基本语法
        /// </summary>
        static void M11Array()
        {
            /*1.创建数组 的 语法
             *  =号左侧的代码 int[] arrInts ,就是在栈空间 开辟了一个 指针大小的空间 用来存放 地址;
             *  =号右侧的代码 new int[3],就是在堆空间 开辟 连续的 3个 int类型大小 的 空间!
             *  =号 将 3个空间的 第1个空间的地址 设置给了 变量 arrInts
             */
            int[] arrInts = new int[3];
 
            //2.使用【下标】为数组赋值
            arrInts[0] = 111;
            arrInts[1] = 777;
            arrInts[2] = 7777;
 
            //3.访问数组里 指定下标 的值
            Console.WriteLine(arrInts[0]);
            Console.WriteLine(arrInts[1]);
            Console.WriteLine(arrInts[2]);
 
        }
        #endregion
 
        #region 1.1 数组习题
        /// <summary>
        /// 1.1 数组习题
        /// </summary>
        static void M10Array()
        {
            //1.要求 保存 用户输入的 n 个学员名字!
            Console.WriteLine("请输入您班上同学的人数:");
            int stuCount = int.Parse(Console.ReadLine());
            //1.1根据 学员人数,创建 一组 字符串类型 的 变量
            string[] names = new string[stuCount];//创建了一组 stuCount 个 字符串类型的 变量
 
            //2.接收学员的名字,存入 数组中
            for (int i = 0; i < stuCount; i++)
            {
                Console.WriteLine("请输入第{0}位学员的名字:", i + 1);
                //?问题,用什么来保存 这 n个学员的名字呢?
                names[i] = Console.ReadLine();
            }
 
            //3.使用for循环 遍历数组 里的 每个 名字
            // 数组有个 Length属性,返回 数组里空间的 个数!(数组长度)
            for (int i = 0; i < names.Length; i++)
            {
                Console.WriteLine("第{0}学员名字:{1}", i + 1, names[i]);
            }
 
        }
        #endregion
 
        #region 题目7:猜测 随机数--如果用户输入的字符串 不能转换成 int 变量,那么 就提示用户输入错误,请他重新输入!(int.TryParse)
        /// <summary>
        /// 题目7:猜测 随机数
        /// </summary>
        static void M07Random()
        {
            /* 猜数字游戏.  随机产生1个1-100之间的数
             * 让用户猜 当用户输入的数比产生的随机数大 就输出 猜大了。
             * 当用户输入的数比产生的随机数小的时候 就输出 猜小了
             * 当用户刚好输入的就是这个随机数的时候 提示成功  并显示用户猜了多少次.*/
            //1.随机产生1个1-100之间的数
            Random ran = new Random();//创建 随机数生成器 对象
            int ranNum = ran.Next(1, 51);//产生一个 1~50 之间的随机数
            Console.WriteLine("随机数已产生成功~~~");
            int guessNum = 0;//用户猜的次数
            //2.用户猜
            while (true)
            {
                Console.WriteLine("请输入您的竞猜数值:");
 
                //接收用户的 输入,并进行 类型监测,如果不是整型的变量,则重新输入!
                string strUsrNum = Console.ReadLine();
                int usrNum = 0;
                //将 字符串strUsrNum 转成 整型变量 赋值给 整型的 usrNum变量;
                //   如果 转换成功,则返回 true
                //   如果 转换失败,则返回 false
                bool isNum = int.TryParse(strUsrNum, out usrNum);
 
                if (!isNum)//如果用户输入的字符串 不能转换成 int 变量,那么 就提示用户输入错误,请他重新输入!
                {
                    Console.WriteLine("请输入数值!");
                    continue;//结束本次循环,进入下一次循环
                }
 
                guessNum++;//记录猜测的次数
 
                if (usrNum == ranNum)
                {
                    Console.WriteLine("恭喜您,猜对了~~送你两个美女~~~~!请笑纳~!");
                    break;
                }
                else if (usrNum > ranNum)
                {
                    Console.WriteLine("您猜的数字 大了~~!");
                }
                else if (usrNum < ranNum)
                {
                    Console.WriteLine("您猜的数字 小了~~!");
                }
            }
            Console.WriteLine("您一共猜了{0}次~~", guessNum);
        }
        #endregion
 
        #region 题目6:用户输入班级的人数. 然后依次输入他们的成绩. 输出总成绩 和 平均成绩.(求和、再求平均值)
        /// <summary>
        /// 题目6:用户输入班级的人数. 然后依次输入他们的成绩. 输出总成绩 和 平均成绩.(求和、再求平均值)
        /// </summary>
        static void M07SumAndAvg()
        {
            int totalScore = 0;
 
            Console.WriteLine("请输入您所在班级的人数:");
            int stuCount = int.Parse(Console.ReadLine());
 
            //循环班级人数次数,依次累加 班级总成绩
            for (int i = 0; i < stuCount; i++)
            {
                Console.WriteLine("请输入第【{0}】位学员的成绩:", i + 1);
                int stuScore = int.Parse(Console.ReadLine());
                totalScore += stuScore; //相当于: totalScore = totalScore + stuScore;
            }
 
            Console.WriteLine("您所在班级总人数为{0},总成绩为:{1},平均分为:{2}", stuCount, totalScore, totalScore * 1.0f / stuCount);
        }
        #endregion
 
        #region 题目5:计算1到100(含)之间的除了能被7整除之外所有整数的和
        /// <summary>
        /// 题目5:计算1到100(含)之间的除了能被7整除之外所有整数的和
        /// </summary>
        static void M06Sum()
        {
            int sum = 0;
            for (int i = 1; i <= 100; i++)
            {
                //如果能被7 整除,则 跳过 【本次循环--continue后的代码不执行,直接进入下次循环】
                if (i % 7 == 0)
                    continue;
 
                sum += i;
            }
            Console.WriteLine("总和为:" + sum);
        }
        #endregion
 
        #region 题目4:用户输入5个数值,最后显示输入的最大值
        /// <summary>
        /// 题目4:用户输入5个数值,最后显示输入的最大值
        /// </summary>
        static void M05Max()
        {
            //最大值 变量
            int maxNum = -1;
            //循环接收 5次 用户的值
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("请输入第【{0}】个数字:", i + 1);
                //接收用户的 数值
                int num = int.Parse(Console.ReadLine());//Convert.ToInt32();
                //判断 用户数值 是否比原来的 最大值大
                if (num > maxNum)
                {
                    //如果大,则把用户数值 作为新的最大值 存起来
                    maxNum = num;
                }
            }
 
            Console.WriteLine("最大值为" + maxNum);
        }
        #endregion
 
        #region 题目3:求1-100之间6的倍数的个数
        /// <summary>
        /// 题目3:求1-100之间6的倍数的个数
        /// </summary>
        static void M04()
        {
            //求1-100之间6的倍数的个数
            int count = 0;
            for (int i = 1; i <= 100; i++)
            {
                if (i % 6 == 0)
                {
                    count++; //count = count + 1;
                }
            }
            Console.WriteLine("1-100之间6的倍数的个数:" + count);
        }
        #endregion
 
        #region 题目2:乘法口诀表
        /// <summary>
        /// 题目2:乘法口诀表
        /// </summary>
        static void M03()
        {
            /*
             * 1*1=1
             * 2*1=2 2*2=4
             * 3*1=3 3*2=6 3*3=9
             * ..............
             * 9*1=9 .............................9*9=81
             */
 
            //1.0 循环行
            for (int row = 1; row <= 9; row++)
            {
                //2.0 根据 当前行 循环打印 当前行里的 列
                for (int col = 1; col <= row; col++)
                {
                    //3.0 输出 列 * 行 =  乘积
                    string strMsg = string.Format("{0}*{1}={2}\t", col, row, (col * row));
                    Console.Write(strMsg);
                }
                Console.Write("\n");
            }
        }
        #endregion
 
        #region 题目1:问do u love me?
        /// <summary>
        /// 题目1:问do u love me?
        /// </summary>
        static void M02()
        {
            //1.凡是 根据条件来循环,就用 while 或者 do while
            //while (true)
            //{
            //    Console.WriteLine("Do u love me ~~~ ?");
            //    string res = Console.ReadLine();
            //    if (res == "yes")
            //    {
            //        break;
            //    }
            //}
 
            //string res = "";
            //do
            //{
            //    Console.WriteLine("Do u love me ~~~ ?");
            //    res = Console.ReadLine();
            //} while (res != "yes");
 
            //2.虽然 for循环也可以完成,但是不建议使用它来根据条件循环; for 循环时专门用来 根据次数循环的!
            for (; ; )
            {
                Console.WriteLine("Do u love me ~~~ ?");
                string res = Console.ReadLine();
                if (res == "yes")
                {
                    break;
                }
            }
 
            Console.WriteLine("thks~~~~ just kidding~~!");
        }
        #endregion
 
        #region 1.0 作业题 void M01Homework()
        /// <summary>
        /// 1.0 作业题
        /// </summary>
        static void M01Homework()
        {
            int a = 110;
            //注意:&& 符号 在执行的时候 是从左到右的执行,遇到任何一个 false,就立即结束运行,并返回 false
            //         &&只有整个表达式里所有的判断都为true,才返回true
            //                    其中任何一个判断 为false,就返回false
            if (a++ > 220 && ++a < 50)
            {
 
            }
            Console.WriteLine(a);
        }
        #endregion
    }
}

  

posted @   枫伶忆  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示