zoukankan      html  css  js  c++  java
  • C#语法快速热身

      1 选择结构(与java结构语法的用法完全一样)
      2 If结构
      3 if(条件)
      4 {
      5     代码块
      6 }
      7 
      8 If-else选择结构
      9 
     10 多重if
     11 If-else if -else
     12 
     13 嵌套if
     14 If(条件)
     15 {
     16     If(条件)
     17     {
     18 
     19 }
     20 }
     21 Else
     22 {
     23     
     24 }
     25 
     26 Switch(条件)
     27 {
     28     Case 常量表达式1:
     29         语句
     30         Break;
     31 Case 常量表达式n:
     32         语句
     33         Break;
     34     Default:
     35 语句
     36         Break;
     37 }
     38 
     39 
     40 数组与循环
     41 一维数组
     42 语法:数组类型[ ] 数组名; -------int[ ] arry;
     43 ⦁    数组类型[ ] 数组名 = new 数组类型[数组长度];
     44 ⦁    数组类型[ ] 数组名 = new 数组类型[ ]{值1,值n};
     45 ⦁    数组类型[ ] 数组名 = {值1,值n};
     46 获取数组长度使用:数组名.Length(同java中一样下标从0开始,C#中叫“索引”)
     47 
     48 XXXXXX-----Int [] srry = new int[5]{0,1,2};// 声明并初始化一维数组
     49 
     50 Java中:错误
     51 Int[ ] num = new int[3]{1,2,3};
     52 
     53 
     54 ⦁    数组类型[ ] 数组名 = new 数组类型[数组长度];
     55 ⦁    数组类型[ ] 数组名 = new 数组类型[数组长度]{值1,值n};
     56 ⦁    数组类型[ ] 数组名 = {值1,值n};
     57 
     58 C#
     59 Int[ ] num = new int[3]{1,2,3};
     60 
     61 
     62 对象数组
     63 讲所有的信息都保存到某个对象中
     64 // 创建Student对象数组,定义3个人
     65             Student[] stu = new Student[3];
     66 
     67             // 实例化
     68             stu[0] = new Student();
     69             stu[0].name = "狗蛋";
     70             stu[0].score = 0;
     71 
     72             stu[1] = new Student();
     73             stu[1].name = "张三";
     74             stu[1].score = 0.1;
     75 
     76             stu[2] = new Student();
     77             stu[2].name = "李四";
     78             stu[2].score = 100;
     79 
     80             Console.WriteLine("学员的信息为:");
     81 
     82             foreach (Student item in stu)
     83             {
     84                 item.showInfo();
     85             }
     86 
     87             Console.ReadKey();
     88 
     89 学员类
     90 public string name;
     91         public double score;
     92         public void showInfo()
     93         {
     94             Console.WriteLine("姓名:" + name + "	成绩 : " + score);
     95         }
     96 
     97 
     98 循环结构
     99 Java中的循环while------do-while------for----foreach(for)
    100 
    101 
    102 C#中的循环
    103 While(条件){代码块}
    104 注:先判断条件是否成立,在进行循环
    105 
    106 Do-while(条件){代码块}
    107 注:先循环,在判断是否成立
    108 
    109 For(表达式1; 表达式2; 表达式3){代码块} 
    110 注:1---2---代码块---3
    111 
    112 Foreach(元素类型 元素的变量名 in 数组){代码块}
    113 对比java中的for()
    114 Int arry[] = {1,3,2};
    115 System.out.println(“-------------------”);
    116 For(int a :arry){
    117     Syso(a);
    118 }
    119 ++I  &&  i++
    120 
    121 Foreach不能改变数组中的元素
    122 数组名++
    123 Foreach遍历----》失败
    124 Console.WriteLine("请输入字符串:");
    125             string strings = Console.ReadLine();
    126             foreach (var item in strings)
    127             {
    128                 Console.WriteLine(item);
    129             }
    130 
    131             Console.ReadKey();
    132  
    133 
    134 
    135 跳转语句:
    136 Break:跳出整个循环,不执行
    137 Continue:跳出本次循环,继续执行下一次,直到循环结束
    138 
    139 
    140 
    141 冒泡排序
    142 越小的值冒泡越高
    143 
    144 框架
    145 For(I = 0;i<N-1;i++)
    146 {
    147     For(j = 0; j<N-1-I;j++)
    148     {
    149         交换的步骤   
    150 }
    151 }
    152 
    153 
    154 冒泡排序口诀(升序)
    155 N个数来排队
    156 两两相比小靠前
    157 外层循环N-1
    158 内层循环N-1-i
    159 
    160             // 长度为5的int类型数组
    161             int[] score = new int[5];
    162 
    163             int i, j;// 循环变量  i比较的轮数   j比较的次数
    164             int temp;// 临时变量
    165 
    166             // 读入成绩
    167             Console.WriteLine("输入5个人的成绩:");
    168             for (i = 0; i < 5; i++)
    169             {
    170                 Console.WriteLine("请输入第{0}位人的成绩:", i + 1);
    171                 // 类型转换
    172                 score[i] = int.Parse(Console.ReadLine());
    173             }
    174 
    175             // 开始排序
    176             // 索引   0  1   2   3   4 
    177             // 值     2  3   4   5   6
    178             // 第一遍i == 0
    179             // 第二轮i == 1
    180             // 第三轮i == 2
    181             // 第四轮i == 3
    182             for (i = 0; i < score.Length - 1; i++)// 外层循环终止条件:数组长度-1
    183             {
    184                 // 内层循环终止条件:数组长度 - 1 - i
    185                 // 索引   0  1   2   3   4 
    186                 // 值     2  3   4   5   6
    187                 //              j < 4
    188                 //              j < 3
    189                 //              j < 2
    190                 //              j < 1
    191                 for (j = 0; j < score.Length - 1 - i; j++)
    192                 {
    193                     // 判断
    194                     if (score[j] > score[j + 1])
    195                     {
    196                         // 元素交换
    197                         temp = score[j];
    198                         score[j] = score[j + 1];
    199                         score[j + 1] = temp;
    200                     }
    201                 }
    202             }
    203             Console.WriteLine("排序后的成绩:");
    204             for (i = 0; i < 5; i++)
    205             {
    206                 Console.WriteLine("{0}	", score[i]);
    207             }
    208 
    209 
    210             Console.ReadLine();
    211 
    212 
    213 
    214 小到大、大到小
    215 // 定义一个数组
    216             int[] nums = new int[]{1,2,3,4,5,6,7,8,9};
    217             int temp = 0;
    218             // 从小到大
    219             for (int i = 0; i < nums.Length - 1; i++)
    220             {
    221                 for (int j = 0; j < nums.Length-1-i; j++)
    222                 {
    223                     if (nums[j] > nums[j + 1])
    224                     {
    225                         temp = nums[j];
    226                         nums[j] = nums[j + 1];
    227                         nums[j + 1] = temp;
    228                     }
    229                 }   
    230             }
    231             foreach (var item in nums)
    232             {
    233                 Console.Write(item + " ");
    234             }
    235 
    236 
    237             Console.WriteLine(" ");
    238 
    239             // 从大到小
    240             for (int i = 0; i < nums.Length - 1; i++)
    241             {
    242                 for (int j = 0; j < nums.Length - 1 - i; j++)
    243                 {
    244                     if (nums[j] < nums[j + 1])
    245                     {
    246                         temp = nums[j];
    247                         nums[j] = nums[j + 1];
    248                         nums[j + 1] = temp;
    249                     }
    250                 }
    251             }
    252             foreach (var item in nums)
    253             {
    254                 Console.Write(item + " ");
    255             }
    256 
    257 
    258 
    259 Var可以理解为匿名类型,认为他是一个变量的占位符,一个数组的类型
    260 特点:
    261 1)必须在定义是初始化(var s = “abc”;    不能:var s; s = “aaaa”)
    262 2)一旦初始化完成,就不能再给变量赋值与初始化
    263 3)var要求的是局部变量
    264 4)var和Object不同,他在效率上和使用强制类型方式定义变量一样
    265 // 声明并赋初始值
    266             var name = "张三";
    267             var age = 10;
    268             var sex = true;
    269 
    270             // 获取变量的数据类型
    271             Type t_Name = name.GetType();// string
    272             Type t_Age = age.GetType(); // int
    273             Type t_Sex = sex.GetType(); // bool
    274 
    275             // 打印结果
    276             Console.WriteLine("变量name的类型是{0}
    ,变量age的类型是{1}
    ,变量sex的类型是{2}", t_Name.ToString(), t_Age.ToString(), t_Sex.ToString());
    277 
    278             // 不自动关闭,等待用户输入
    279             Console.ReadLine();
    280  
  • 相关阅读:
    vue 中 vue-router、transition、keep-alive 怎么结合使用?
    vue 对列表数组删除和增加
    eclipse如何快速查找某个类
    在 eclipse 中设置每行的字数
    如何查看某个端口被谁占用
    sql只修改第一二行数据
    android真机自动化测试
    appium自动化测试中获取toast消息的解决方法【转】
    eclipse下python的selenium自动化环境的搭建
    Xpath用法官方手册
  • 原文地址:https://www.cnblogs.com/www-yang-com/p/8137192.html
Copyright © 2011-2022 走看看