【C#】课堂知识点#2

课堂上老师讲了几点,自己觉得挺重要的,记录下来

1、代码字体调大,方便调试

2、虚心请教,没有谁比谁厉害,不会就虚心多请教,baidu并不能解决所有问题。沟通交流也是一种能力

3、只有每行写对了,才继续往下写,写的时候必须善用“Tab”键。

4、多写笔记,善于利用“便签”,“时间管理”

 

 

1、代码自动缩进

 


 

 

2、选择结构

 

a、if …… else if …… else

if()
{
    .........
}
else if()
{
    .........
}
else()
{
    .........
}    

 

 

b、switch 语句

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             // 提示词是得分点
13             Console.WriteLine("请输入一个数字");
14 
15             // Parse , 语句嵌套 实现 输入数字 赋给 x
16             int x = int.Parse(Console.ReadLine());
17 
18             // 注意不能直接定义而不给初始值
19             string res = "";
20             //Console.WriteLine(x);
21 
22             if( x < 0 || x > 100)
23             {
24                 Console.WriteLine("您输入的数字不合法");
25             }
26             else
27             {
28                 switch (x / 10)
29                 {
30                     case 10:    
31                     case 9: res = "A"; break;
32                     case 8: res = "B"; break;
33                     case 7: res = "C"; break;
34                     case 6: res = "D"; break;
35                     default: res = "E";break;
36                 }
37                 Console.WriteLine("{0}的等级为:{1}",x,res);
38             }
39         }
40     }
41 }

 

 

Array类的使用,以及Random类的使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             // 随机数,Random , 系统提供类
13             Random R = new Random();
14             int[] a = new int[100];
15             for( int  i = 0; i < 10; i++)
16             {
17             //  Next 返回一个 Next( Int32 ) -> 随机生成小于 101 非负整数。
18             //  Next ( L , R )  随机生成 [L,R)里面的整数的
19                 a[i] = R.Next(101);
20                 //a[i] = R.Next(1, 3);
21                 if ((i + 1) % 5 == 0) Console.WriteLine() ;
22             }
23 
24             // foreach -> 针对里面赋值了的元素进行输出
25             foreach (var item in a )
26             {
27                 Console.Write( "{0}\t",item );    
28             }
29             Console.WriteLine( "\n______________" );
30             Console.WriteLine();
31 
32             // Array 类自带排序
33             Array.Sort(a);
34             foreach (var item in a)
35             {
36                 Console.Write("{0}\t", item);
37             }
38             Console.WriteLine("\n______________");
39             Console.WriteLine();
40 
41             // Array 数组的逆置
42             Array.Reverse(a);
43             foreach (var item in a)
44             {
45                 Console.Write("{0}\t", item);
46             }
47 
48             //[,] 加逗号划分维度
49             int[,] B = new int[10, 20];
50 
51             Console.WriteLine("\n_______________");
52 
53             //返回整个数组的大小
54             Console.WriteLine( B.Length );
55 
56             //返回数组的维度
57             Console.WriteLine( B.Rank );
58 
59             //返回数组 两个不同维度的长度
60             Console.WriteLine( B.GetLength(0) );
61             Console.WriteLine( B.GetLength(1) );
62         }
63     }
64 }

 

 

String 类的Split使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = "1 2 3 4 , 5 ,6 7 . 5 4";
13             char[] t = new char[] { ',', '.', ' ' };
14             string[] str1 = s.Split( t , StringSplitOptions.RemoveEmptyEntries);
15             string[] str2 = s.Split( new char[] { ',','.',' '} , StringSplitOptions.RemoveEmptyEntries);
16             foreach (var item in str1 )
17             {
18                 Console.WriteLine("{0}",item);
19             }
20         }
21     }
22 }

 

posted @ 2019-09-20 19:30  Osea  阅读(203)  评论(0编辑  收藏  举报