C#语言小结

数据类型--变量与常量--运算符与表达式--语句(if,for)--数组--函数--结构体

一、数据类型:
(一)内建类型
整型(int short long byte uint ushort ulong sbyte),浮点(double float decimal),布尔(bool),字符(char)

对于整型和浮点型都有个ToString("格式化字符串"):
#——任意一个数字。有的话就显示,没有就不显示。
0——必须有一个数字,没有的话就补零。
.——小数点
,——千位分隔。

(二)常用的类
Math DateTime string
Math:
Math.Ceiling(double ):大于当前小数的最小整数。
Math.Floor(double):小于当前小数的最大整数。
Math.Round(double):四舍五入
DateTime:
Year,Month,Day,Hour,Minute,Second,MilliSecond,DayOfWeek,DayOfYear
AddYears(n),AddMonths(),AddDays().........
ToString("格式化字符串"):格式显示。
yyyy,yy——年份。MM,M——月份。dd,d——天。hh,h——时。mm,m——分。ss,s——秒。ms——毫秒

(三)自定义类型
struct

(四)数据类型转换

 

1.自动转换(隐式转换)

 

  a.两者数据类型相兼容

 

  b.目标类型大于源类型  

 

  c.字符类型可以转换为整型(字符所对应的ASCII码)

 

  d.字符类型与整型数字参与数学运算或者比较运算,会将字符类转换为整型再参与运算. 

 

2.强制转换(显示转换)

 

  a.语法:(目标数据类型) 待转换的数据。如:int  a=(int)3.14;

 

  b.强制类型转换,数据类型一定要相兼容 

 

3.自动转换和强制转换的区别?

 

   a.自动转换不会丢失精度。

 

   b.强制转换可能会丢失精度(如数据溢出)。

 

4.Parse()和Convert转换

 

  语法:数据类型.Parse(string str);

 

           Convert.To数据类型(string str);

 

注意:任何数据类型都有.Parse()这个方法而Convert不是,如:我们定义的结构类型,枚举等都没有Convert。Convert只有C#内置的数据类型才有。

 

二、变量与常量:
(一)变量就是装数据容器。——U盘
定义:

什么叫做变量?

我们把值可以改变的量叫做变量。

变量的声明:

           语法:[访问修饰符] 数据类型 变量名; 如: int number=10;//声明了一个整型的变量number。

           注意:一次声明多个变量之间要用逗号分隔。

           如:int number1,number2,number3....;


数据类型 变量名[ = 值],变量名[ = 值],....;
int a,b; int a = 5,b;
变量的命名规则:
1.变量名只能由字母、数字、下划线组成
2.只能字母,下划线开头
3.不能与关键词重复。
赋值:
变量名=值;——注意:变量类型与等号右边的值类型相一致。不一致就需要进行类型转换。

如: int number=100;

同时声明多个变量并在声明时赋值 如:int a=1,b=2,c=3;
类型转换:
1.自动转换:一般来说自动转换,只要不存在丢数据的可能性,计算就会自动转化。例如:double a = 3+5.0;
2.强制转换:只要存在丢数据的可能性,计算机就不给自动转化,需要手动强制转化。
Convert.Toxxx(); Convert.ToInt32();
double a = 3.14;
int b = (int)a;
取值:直接写变量名。

局部变量(重点):

一定要:先声明,再复制,最后使用。


(二)常量:常量也是装数据的容器,但在运算过程中常量不能放在单等的左边。——一次性光盘
分类:字面常量,符号常量。
定义:const int PI = 3.14;
注意:常量在定义的时候必须要赋值。
取值:直接使用常量取值。

三、运算符:
算术,关系,逻辑,其它
(一)算术——7
+ - * / % ++ --
整数除整数还是整数。

注意:++,--两个一元运算符都有前和后两种方式,前加(前减)都是在原值上先加1(减1)再计算,后加(后减)都是先用原值计算再给原值进行加1(减1);

总结:不管是前加(前减)还是后加(后减),最终都在原值上进行了加1(减1).

优先级:先乘除,后加减,有括号先算括号里的,相同级别的从左至右运算。小括号可以无限制的套用,但一定要成对出现。
(二)关系——6
== != > >= < <=
(三)逻辑——3
&& || !
(四)其它
1.复合运算符:+= -= *= /= %=
2.赋值: =
3.条件运算符:表达式1?表达式2:表达式3

常用:+=一般用于求和,*=一般用于求某个数以一定倍数增长到某个时候的值。(如:某个数每一年以25%增长,问6年后是多少?)

注意:能够改变变量中的值的运算符有:

                     1>赋值符“=”(包含了上面的+=,-=等五种)

                     2>自加自减运算符,++ --

四、语句:顺序、分支、循环
(一)分支——if
1.  if(表达式)
{

语句1;
}

执行过程:首先判断条件的结果,若结果为True则执行语句1,若为false则跳过语句1,执行后面的语句。

  注意:if后面括号中的条件,要能计算成一个Bool类型的值,默认情况下if语句只能带一句话(和if有关的语句只有语句1)在If语句中如果想让if带多句话则用{}把if带的多句话组成语句块。

2.if(表达式)
{

语句1;
}
else
{

语句块2;
}

执行过程:如果if的条件为true则执行if带的语句块1,并且跳过else带的语句2。若条件为false则是跳过if带的语句块1,执行else带的语句块2。

3.if-else-if结构

  语法:if(条件1)

          {

              语句块1;

          }

          else if(条件2)

          {

              语句块2;

          }

          ……

          else if(条件n)

          {

              语句块n;

          }else

          {

              语句块n+1;

          }

  执行过程:在if-else-if语句中,只有当上一个条件不成立时,才会进入下一个if语句,并进行If语句后面的条件判断,一旦有一个if后面的条件为true则执行此if所带的语句块,语句块执行完成后程序跳出

  if-else-if,if-else-if中如果所有条件都不成立,则执行最后的else(可有可无)。else与离得最近的if是一对。

 

例子:
1.判断闰年,平年

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 闰平年
 9     {
10         static void Main(string[] agre)
11         {
12             Console.Write("请输入一个年份:");
13             string a = Console.ReadLine();
14             int year = Convert.ToInt32(a);
15 
16             if (year % 400 == 0)
17             {
18                 Console.WriteLine(year + "年是闰年");
19             }
20             else if (year % 4 == 0 && year % 100 != 0)
21             {
22                 Console.WriteLine(year + "年是闰年");
23                
24             }
25             else
26             {
27                 Console.WriteLine(year + "年是平年");
28             }
29         }
30     }
31 }

 


2.一元二次方程根的情况

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //输入
13             Console.Write("分别输入a,b,c三个数");
14             string a1 = Console.ReadLine();
15             string b1 = Console.ReadLine();
16             string c1 = Console.ReadLine();
17             int a = Convert.ToInt32(a1);
18             int b = Convert.ToInt32(b1);
19             int c = Convert.ToInt32(c1);
20 
21             if (a == 0)
22             {
23                 Console.WriteLine("不是一元二次方程");
24             }
25             else
26             {
27                 int d = b * b - 4 * a * c;
28                 if (d < 0)
29                 {
30                     Console.WriteLine("一元二次方程无实根");
31                 }
32                 else if (d == 0)
33                 {
34                     Console.WriteLine("一元二次方程有两个相同实根");
35                 }
36                 else
37                 {
38                     Console.WriteLine("一元二次方程有两个不同实");
39                 }
40             }
41 
42         }
43     }
44 }

 


3.男女体重与身高

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 体重
 9     {
10         static void Main(string[] agre)
11         {
12             Console.Write("请输入你的性别(男,女):");
13             string sex = Console.ReadLine();
14             Console.Write("请输入你的身高(cm):");
15             string h = Console.ReadLine();
16             int high = Convert.ToInt32(h);
17             Console.Write("请输入你的体重(KG):");
18             string m = Console.ReadLine();
19             int t = Convert.ToInt32(m);
20 
21             if (sex == "")
22             {
23                 if (t > high - 100 + 3) 
24                 {
25                     Console.WriteLine("你的体重偏胖。");
26                 }
27                 else if (t < high - 100 - 3)
28                 {
29                     Console.WriteLine("你的体重偏瘦。");
30                 }
31                 else if (t >=high - 100 -3&& t<= high -100 +3)
32                 {
33                     Console.WriteLine("你的体重正常");
34                 }
35             }
36             else if (sex == "")
37             {
38                 if (t > high - 110 + 3)
39                 {
40                     Console.WriteLine("你的体重偏胖。");
41                 }
42                 else if (t < high - 110 - 3)
43                 {
44                     Console.WriteLine("你的体重偏瘦。");
45                 }
46                 else
47                 {
48                     Console.WriteLine("你的体重正常");
49                 }
50             }
51             else
52             {
53                 Console.WriteLine("输入的不正确,请核实。");
54             }
55         }
56     }
57 }

 


4.判断日期是否正确。


(二)循环
1.语法

for(初始条件;循环条件;变量改变)
{
 执行语句;(循环体)
}
循环的四要素:

循环的嵌套:打印星号。

两类问题:迭代法,穷举法。

2. 执行过程:

   1. 计算表达式1转向2

   2. 计算表达式2(循环条件)若为true转向3,false转向5

   3. 执行循环体转向4

   4. 执行表达式3转向2

   5. 循环结束

一般情况下:

      表达式1:用于定义循环变量和对循环变量赋初值

      表达式2:循环条件

      表达式3:用于改变循环条件的值

注意:一般情况下for循环用于已知次数的循环中。

迭代法: 按照某种规律通过循环迭代求解。
求100以内数的和,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         //前100个数的和
11         static void Main(string[] args)
12         {
13             int sum = 0;
14             for (int i = 1; i <= 100;i++ )
15             {
16                 sum = sum + i;
17             }
18             Console.WriteLine(sum);
19         }
20     }
21 }

 

求阶乘。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 求阶乘
 9     {
10         //求阶乘
11         static void Main(string[] args)
12         {
13             int jieCheng = 1;
14             for (int i = 1; i <= 5; i++ )
15             {
16                 jieCheng = jieCheng * i;
17             }
18             Console.WriteLine(jieCheng);
19         }
20     }
21 }

 


1.5个小孩子求年龄

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class 年龄
    {
        static void Main(string[] args)
        {
            int age = 16;
            for (int i = 5; i >= 1;i-- )
            {
                age = age - 2;
            }
            Console.WriteLine(age);
        }
    }
}

 


2.棋盘上放粮食


3.折纸与珠峰的高度。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     //一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?
 9     //一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?
10     class Class3
11     {
12         static void Main(string[] args)
13         {
14             double h = 0.00015;
15             for (int i = 1; i <= 26; i++)
16             {
17                 h = h * 2;
18             }
19             Console.WriteLine(h);
20         }
21     }
22 }

 


4.落球问题。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 落球问题
 9     {
10         //一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起的高度是多少?
11         static void Main(string[] args)
12         {
13             double h = 10;
14             for (int i = 1; i <= 5;i++ )
15             {
16                 h = h * 2 / 3;
17             }
18             Console.WriteLine(h);
19         }
20     }
21 }

 


5.猴子吃桃子

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 猴子桃
 9     {
10         //公园里有一只猴子,和一堆的桃子,猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的,到第七天猴子发现只有一个桃子了。
11         static void Main(string[] args)
12         {
13             int s = 1;//第7天的桃子数
14             for (int i = 6; i >= 1;i-- )
15             {
16                 s = (s + 1) * 2;//前一天的桃子叔=今天的桃子数+扔掉的一个+昨天猴子吃掉的一半
17             }
18             Console.WriteLine(s);
19         }
20     }
21 }

 


6.兔子生兔子。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 兔子
 9     {
10         //一一对兔新生兔子,到第三个月开始生小兔,以后每个月都会生一对小兔,小兔不断长大也会生小兔,每次生一公一母,问第十二个月底有多少小兔。
11         static void Main(string[] args)
12         {
13             int tu1 = 1, tu2 = 1;//tu1是倒数第一个月的兔子数,tu2是倒数第二个月的兔子数,斐波那契数列
14             int tu=0;//
15 
16             for (int i = 3; i <= 24;i++ )
17             {
18                 tu = tu1 + tu2;
19                 tu2 = tu1;
20                 tu1 = tu;
21             }
22             Console.WriteLine(tu);
23         }
24     }
25 }

 

穷举法:把所有的情况都走一遍,根据条件筛选。
求100以内与7相关的数。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class5
 9     {
10         static void Main(string[] args)
11         {
12             int i = 1;
13             int count = 0; //记录与7有关的数字的个数
14             while (i <= 100)
15             {
16                 if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
17                 {
18                     Console.Write(i + "\t");
19                     count++;
20                     //1
21                 }
22                 i++;
23                 //2
24             }
25             //3
26             Console.Write("共有" + count + "个与7相关的数");
27         }
28     }
29 }

 


1.买东西。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 元旦
 9     {
10         //购物券100元,香皂(5元),牙刷(2元),洗发水(20元)。正好花完,怎么花?
11         static void Main(string[] args)
12         {
13             for (int a=0;a<=20;a++)
14             {
15                 for (int b = 0; b <= 50; b++)
16                 {
17                     for (int c = 0; c <= 5;c++ )
18                     {
19                         if (5*a+2*b+20*c==100)
20                         {
21                             Console.WriteLine("应该买香皂"+a+"块,牙刷"+b+"只,洗发水"+c+"瓶。");
22                         }
23                     }
24                 }
25             }
26         }
27     }
28 }

 


2.百鸡百钱,百马百石。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 百钱白鸡
 9     {
10         //有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡一文钱一只,小鸡半文钱一只。如何买?
11         static void Main(string[] args)
12         {
13             for (int a = 0; a <= 50;a++ )
14             {
15                 for (int b = 0; b <= 100;b++ )
16                 {
17                     for (double c = 0; c <= 200;c++ )
18                     {
19                         if(a*2+b*1+c*0.5==100&&a+b+c==100)
20                         {
21                             Console.WriteLine("公鸡"+a+"只,母鸡"+b+"只,小鸡"+c+"只。");
22                         }
23                     }
24                 }
25             }
26         }
27     }
28 }

 


3.侦察兵

某侦察队接到一项紧急任务,要求在A、B、C、D、E、F六个队员中尽可能多地挑若干人,但有以下限制条件:
A和B两人中至少去一人;                                   a+b>=1
A和D不能一起去;                                           a+d<=1
A、E和F三人中要派两人去;                              a+e+f==2
B和C都去或都不去;                                        b+c!=1
C和D两人中去一个;                                        c+d==1
若D不去,则E也不去。                                      d+e==0||d==1
问应当让哪几个人去?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 侦察队
 9     {
10         static void Main(string[] args)
11         {
12             for (int a = 0; a <= 1;a++ )
13             {
14                 for (int b = 0; b <= 1;b++ )
15                 {
16                     for (int c = 0; c <= 1;c++ )
17                     {
18                         for (int d = 0; d <= 1;d++ )
19                         {
20                             for (int e = 0; e <= 1;e++ )
21                             {
22                                 for (int f = 0; f <= 1;f++ )
23                                 {
24                                     if (a + b >= 1 && a + d <= 1 && a + e + f == 2 && b + c != 1 && c + d == 1 && (d + e == 0 || d == 1))
25                                     {
26                                         Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d+";e="+e+";f="+f);
27                                     }
28                                 }
29                             }
30                         }
31                     }
32                 }
33 
34             }
35         }
36     }
37 }

 


4.求等式。123()45()67()8()9=100;要求在()里面填写+或-使等式成立。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class Class1
 9     {
10         //123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
11         static void fff(string[] args)
12         {
13             for (int a = -1; a <= 1;a=a+2 )//-1代表减号,1代表加号
14             {
15                 for (int b = -1; b <= 1;b=b+2 )
16                 {
17                     for (int c = -1; c <= 1;c=c+2 )
18                     {
19                         for (int d = -1; d <= 1;d=d+2 )
20                         {
21                             if (123+a*45+67*b+8*c+9*d==100)
22                             {
23                                 Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d);
24                             }
25                         }
26                     }
27                 }
28             }
29         }
30     }
31 }

 (三)break,continue,goto跳转语句

1. break:

    a.可以用于switch-case中,跳出switch。

    b.用在循环中,用来立即跳出(终止)循环(跳出的是当前循环)。

2. continue

    continue用于循环中,程序一旦执行到continue就立即结束本次循环直接进入下一次循环(for有点特殊,先对循环变量操作后在判断进行)。

3. goto

          语法:

                  语句1;

                  goto lable;

                  语句2;

                  ……..

                  语句n;

                  lable:;

                 Console.WriteLine(“我只执行了语句1其它的都跳过了!”);

                 Console.ReadKey();

    文字叙述语法:先定义一个标签面后面加冒号(如:lable:),再goto标签名,程序将直接跳到标签名后面的语句继续执行。


五、数组:
思想:解决大量同类数据存储和操作的问题。
特点:连续,同一类数据。
分类:一维数组,二维数组,多维数组。

数组作用:数组可以一次帮我们声明多个相同类型的变量,这些变量在内存中是连续存储的。

一维数组:
定义:
数据类型[] 数组名 = new 数据类型[数组的长度] [{初始化}];
赋值:
数组名[下标] = 值;
可以与循环结合起来。
取值:
数组名[下标];
可以与循环结合起来。

数组初始化器“{}“的用法:

                                                           a.Int[] number={1,3,9}//声明时赋值.

                                                           b.Int[] number=new int[3]{5,8,3}(注:初始化器里面的元素个数必须跟数组的长度相等,若不相等就会报错) 如下:

                                                              Int[] number=new int[5]{5,8,3}->错的!初始化器里面的元素个数是3数组长度是5,因3!=5所以报错。

                                                           c.Int[] number=new int[]{5,8,3}->对的。因为初始化器会自动帮我们判断数组长度,所以可以不写数组长度。

例子:
1.球员打分

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[6];
13             Console.WriteLine("********球员训练记录********");
14 
15             for (int i=0;i<a.Length;i++)
16             {
17                 Console.Write("请输入第"+(i+1)+"个球员的成绩:");
18                 a[i] =Convert.ToInt32( Console.ReadLine());
19             }
20 
21             for (int m = 0; m < a.Length; m++)
22             {
23                 Console.WriteLine(""+(m+1)+"个球员的成绩是"+a[m]+"");
24             }
25 
26             int sum = 0;
27             double ave = 0; 
28             for (int n = 0; n < a.Length;n++ )
29             {
30                 sum = sum + a[n];
31 
32             }
33             ave = 1.0 * sum / a.Length;
34             Console.WriteLine("球员总成绩是"+sum+",平均成绩是"+ave+"");
35 
36             int max = 0,min=200;
37             int maxSub = -1, minSub = -1;
38             for (int b = 0; b < a.Length;b++ )
39             {
40                 max = max > a[b] ? max : a[b];
41                 maxSub = b;
42             }
43             for (int c = 0; c < a.Length; c++)
44             {
45                 if(min>a[c])
46                 {
47                     min=a[c];
48                     minSub=c;
49                 }
50             }
51             Console.WriteLine((maxSub+1)+"球员中最高成绩是" + max + ","+(minSub+1)+"最差成绩是"+min+"");
52 
53 
54         }
55     }
56 }

 


2.选班长

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Class1
    {
        static void bbb(string[] args)
        {
            //30人投票,从5个候选人选一个出来。
            int[] vote = new int[5];
            for(int i=0;i<30;i++)
            {
                Console.Write("请第"+(i+1)+"位同学投票(0-4):");
                int temp = Convert.ToInt32(Console.ReadLine());
                if(temp <0 || temp >4)
                {
                    Console.WriteLine("废票");
                    continue;
                }
                else
                {
                    vote[temp]++;
                }
            }

            //计算最终得票。
            int max = 0, maxSub = 0;
            for(int i=0;i<vote.Length;i++)
            {
                //把每位候选人的票数显示出来。
                Console.WriteLine("" + (i + 1) + "号候选人的票数是:" + vote[i]); 
                //计算最大值。
                if(vote[i] > max)
                {
                    max = vote[i];
                    maxSub = i;
                }
            }

            //显示最终结果。
            Console.WriteLine("最终投票结果为:"+(maxSub+1)+"号候选人当选,当选票数是"+max+"票。");
        }
    }
}

 


3.36选7

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class3
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[7];
13 
14             Random rand = new Random();
15             for (int i = 0; i < a.Length; i++)//7-代表要生成7个不同的数
16             {
17                 //生成一个随机数
18                 int n = rand.Next(36);
19                 n++;
20 
21                 //查重
22                 bool chong = false;
23                 for (int j = 0; j < a.Length;j++ )
24                 {
25                     if (n==a[j])
26                     {
27                         chong = true;
28                         break;
29                     }
30                 }//for
31                 //确定n合不合理
32                 if (chong == false)
33                 {
34                     a[i] = n;
35                 }
36                 else
37                 {
38                     i--;
39                 }
40             }//for
41 
42             //显示彩票号
43             for (int k = 0; k < a.Length;k++ )
44             {
45                 Console.Write(a[k]+"\t");
46             }
47         }//Main
48     }
49 }

 


3.青歌赛。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[10];
13             //亮分
14             ShuRu(a);
15 
16             //排序
17             PaiXu(a);
18 
19             //运算求平均
20             double avg = YunSuan(a);
21 
22             //输出显示
23             ShuChu(a, avg);
24         }
25 
26         private static void ShuChu(int[] a, double avg)
27         {
28             Console.WriteLine("去掉两个最高分:" + a[0] + "" + a[1]);
29             Console.WriteLine("去掉两个最低分:" + a[a.Length - 1] + "" + a[a.Length - 2]);
30             Console.WriteLine("该选手最终得分为:" + avg);
31         }
32 
33         private static double YunSuan(int[] a)
34         {
35             //求总分
36             int sum = 0;
37             for (int i = 2; i <= a.Length - 3; i++)
38             {
39                 sum += a[i];
40             }
41             //求平均
42             double avg = (1.0 * sum) / (a.Length - 4);
43             return avg;
44         }
45 
46         private static void PaiXu(int[] a)
47         {
48             for (int i = 1; i <= a.Length - 1; i++)
49             {
50                 for (int j = 1; j <= a.Length - i; j++)
51                 {
52                     if (a[j] > a[j - 1])
53                     {
54                         int temp = a[j];
55                         a[j] = a[j - 1];
56                         a[j - 1] = temp;
57                     }
58                 }
59             }
60         }
61 
62         private static void ShuRu(int[] a)
63         {
64             for (int i = 0; i < a.Length; i++)
65             {
66                 Console.Write("请第" + (i + 1) + "号评委亮分:");
67                 a[i] = Convert.ToInt32(Console.ReadLine());
68             }
69         }
70     }
71 }

 

二维数组:

二维数组: 是用来声明指定行指定列的1个表格结构.

                               a.语法:数据类型[,] 数组名称=new 数据类型[行数,列数]

                                           如:int[,] arr = new int[3, 4];

                                           当执行到这句话的时候,就表示声明了1个3行4列的1个表格结构数据。

                               b.元素个数:二维数组的元素个数=行数*列数;

                               c.为元素赋值:数组名[行数,列数]=值;

                               d.取值:与赋值相对应。1>即:数组名[行数,列数];

                                                                2>通过函数GetValue(行数,列数);

                               e.Length 属性的值代表数组中元素的个数,若是二维数组Length的值等于=行数*列数;

                               f.数组有个GetLength(参数)方法,传入0得到行数,传入1得到列数。(GetLength中参数的本质是维度,该方法即:得到指定维度的长度)

                               g.利用数组初始化器为二维数组赋值:

                                                                             如:int[,] arr = { { 1, 1, 3, 5 }, { 4, 5, 6, 4 }, { 7, 8, 9, 6 } };

                               h. 数组的Rank属性可以得到数组的维度。

                                                                            如:Console.WriteLine(arr.Rank);就会输出2

                               i.遍历:foreach最简单,直接遍历值。

                                                                             如:使用for循环遍历二维数组

                                                                                                                       for(int i =0;i<arr.GetLength(0);i++)

                                                                                                                      {

                                                                                                                           for(int j = 0;j<arr.GetLength(1);j++)

                                                                                                                          {

                                                                                                                                arr[i,j]

                                                                                                                          }      

                                                                                                                      }


例子:
1.学生成绩。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         static void Main(string[] args)
11         {
12             int[,] a = new int[3, 4];
13             //输入
14             for (int i = 0; i < 3;i++ )
15             {
16                 a[i, 0] = i + 1;
17                 Console.Write("语文:");
18                 a[i, 1] = Convert.ToInt32(Console.ReadLine());
19                 Console.Write("数学:");
20                 a[i, 2] = Convert.ToInt32(Console.ReadLine());
21                 a[i, 3] = a[i, 1] + a[i, 2];
22             }
23             //输出
24             Console.WriteLine("学号\t语文\t数学\t总分\t");
25             for (int n = 0; n < 3;n++ )
26             {
27                 for (int m = 0; m < 4;m++ )
28                 {
29                     Console.Write(a[n,m]+"\t");
30                 }
31                 Console.WriteLine();
32             }
33         }
34     }
35 }

 


2.推箱子。

两个应用:二分法查找,冒泡排序。
二分法查找思想:前提是数组有序,每次找中间的值对比,否满足条件就扔一半。
使用最大下标max、最小下标min,中间值下标mid,控制查找的范围。 mid = (max+min)/2; max = mid+1; min = mid-1;
如果一直查到min>max就结束了,说明没有找到。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[] {3,5,7,9,13,14,18 };
13             Console.Write("请输入要找的数:");
14             int find=Convert.ToInt32(Console.ReadLine());
15 
16             int top, bottom, mid;//上限下标,下限下标,中间下标
17             top = 0;
18             bottom = a.Length - 1;
19 
20             while (bottom >= top)//只要下限下标还在上限下标的下面,就循环,否则没找到就结束。
21             {
22                 //算中间下标
23                 mid = (top + bottom) / 2;
24                 //取中间的值
25                 int n = a[mid];
26                 if(n<find)
27                 {
28                     top=mid+1;//调整上限的下标
29                 }
30                 else if(n>find)
31                 {
32                     bottom = mid - 1;//调整下限的下标
33                 }
34                 else
35                 {
36                     Console.WriteLine("找到了,在第"+mid+"下标处找到"+find);
37                     break;
38                 }
39             }
40         }
41     }
42 }

 

冒泡排序的思想:相邻两个数进行依次对比,互换。
两层循环,外层循环趟数,内层循环每趟的次数。
趟数:n-1
次数:n-i
for(int i=1;i<=n-1;i++)
{
for(int j=1;j<=n-i;j++)
{
if(a[j] > a[j-1])
{
互换。
}
}
}

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 };
13             //冒泡排序。
14             for(int i=1;i<=a.Length-1;i++) //趟数
15             {
16                 for (int j = 1; j <= a.Length - i; j++)//次数
17                 {
18                     if(a[j-1] > a[j])
19                     {
20                         int t = a[j - 1];
21                         a[j - 1] = a[j];
22                         a[j] = t;
23                     }
24                 }
25             }
26 
27             //显示
28             for(int k=0;k<a.Length;k++)
29             {
30                 Console.WriteLine(a[k]);
31             }
32         }
33     }
34 }

 

六、函数:
什么是函数:能够完成某个独立功能模块就可称之为函数。
为什么要用函数:结构清晰,分工开发,代码重用。
四要素:函数名,形参,返回类型,函数体。
定义语法:
返回类型 函数名(形参列表)
{
函数体
}
调用:
函数名(实参列表);
数据类型 变量名 = 函数名(实参列表);

函数的传值与传址的问题。
1.内建类型,日期时间都是默认传值。 ——ref
2.数组,字符串默认都是传址。

函数的返回值。——return 值或变量;要保持return后面的类型与函数的返回类型要一致。

递归。自己调自己。
语法思想:
返回类型 函数名(参数)
{
1.结束递归的判断。
2.递归运算:函数名(参数);
}

七、结构体:
为什么要用结构体?自己定义的复合类型,更好地模拟生活中的各种对象。

定义
struct 结构体名
{
public 类型 子变量名;
public 类型 子变量名;
....
}

使用:
结构体名 结构体变量 = new 结构体名();
结构体变量.子变量 = 值;
结构体变量.子变量;

结构体数组:
结构体类型[] 数组名 = new 结构体类型[长度];
数组名[下标].子变量

如何使用循环来操作结构体数组。

例子:学生成绩统计。对战的小游戏。

 

posted on 2015-01-06 14:26  酸甜sky  阅读(402)  评论(0编辑  收藏  举报