数据类型--变量与常量--运算符与表达式--语句(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.判断闰年,平年

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 闰平年
{
static void Main(string[] agre)
{
Console.Write("请输入一个年份:");
string a = Console.ReadLine();
int year = Convert.ToInt32(a); if (year % == )
{
Console.WriteLine(year + "年是闰年");
}
else if (year % == && year % != )
{
Console.WriteLine(year + "年是闰年"); }
else
{
Console.WriteLine(year + "年是平年");
}
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//输入
Console.Write("分别输入a,b,c三个数");
string a1 = Console.ReadLine();
string b1 = Console.ReadLine();
string c1 = Console.ReadLine();
int a = Convert.ToInt32(a1);
int b = Convert.ToInt32(b1);
int c = Convert.ToInt32(c1); if (a == )
{
Console.WriteLine("不是一元二次方程");
}
else
{
int d = b * b - * a * c;
if (d < )
{
Console.WriteLine("一元二次方程无实根");
}
else if (d == )
{
Console.WriteLine("一元二次方程有两个相同实根");
}
else
{
Console.WriteLine("一元二次方程有两个不同实");
}
} }
}
}

3.男女体重与身高

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 体重
{
static void Main(string[] agre)
{
Console.Write("请输入你的性别(男,女):");
string sex = Console.ReadLine();
Console.Write("请输入你的身高(cm):");
string h = Console.ReadLine();
int high = Convert.ToInt32(h);
Console.Write("请输入你的体重(KG):");
string m = Console.ReadLine();
int t = Convert.ToInt32(m); if (sex == "男")
{
if (t > high - + )
{
Console.WriteLine("你的体重偏胖。");
}
else if (t < high - - )
{
Console.WriteLine("你的体重偏瘦。");
}
else if (t >=high - -&& t<= high - +)
{
Console.WriteLine("你的体重正常");
}
}
else if (sex == "女")
{
if (t > high - + )
{
Console.WriteLine("你的体重偏胖。");
}
else if (t < high - - )
{
Console.WriteLine("你的体重偏瘦。");
}
else
{
Console.WriteLine("你的体重正常");
}
}
else
{
Console.WriteLine("输入的不正确,请核实。");
}
}
}
}

4.判断日期是否正确。

(二)循环
1.语法

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

循环的嵌套:打印星号。

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

2. 执行过程:

1. 计算表达式1转向2

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

3. 执行循环体转向4

4. 执行表达式3转向2

5. 循环结束

一般情况下:

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

表达式2:循环条件

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

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

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
//前100个数的和
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <= ;i++ )
{
sum = sum + i;
}
Console.WriteLine(sum);
}
}
}

求阶乘。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 求阶乘
{
//求阶乘
static void Main(string[] args)
{
int jieCheng = ;
for (int i = ; i <= ; i++ )
{
jieCheng = jieCheng * i;
}
Console.WriteLine(jieCheng);
}
}
}

1.5个小孩子求年龄

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 年龄
{
static void Main(string[] args)
{
int age = ;
for (int i = ; i >= ;i-- )
{
age = age - ;
}
Console.WriteLine(age);
}
}
}

2.棋盘上放粮食

3.折纸与珠峰的高度。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
//一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?
//一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?
class Class3
{
static void Main(string[] args)
{
double h = 0.00015;
for (int i = ; i <= ; i++)
{
h = h * ;
}
Console.WriteLine(h);
}
}
}

4.落球问题。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 落球问题
{
//一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起的高度是多少?
static void Main(string[] args)
{
double h = ;
for (int i = ; i <= ;i++ )
{
h = h * / ;
}
Console.WriteLine(h);
}
}
}

5.猴子吃桃子

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 猴子桃
{
//公园里有一只猴子,和一堆的桃子,猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的,到第七天猴子发现只有一个桃子了。
static void Main(string[] args)
{
int s = ;//第7天的桃子数
for (int i = ; i >= ;i-- )
{
s = (s + ) * ;//前一天的桃子叔=今天的桃子数+扔掉的一个+昨天猴子吃掉的一半
}
Console.WriteLine(s);
}
}
}

6.兔子生兔子。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class 兔子
{
//一一对兔新生兔子,到第三个月开始生小兔,以后每个月都会生一对小兔,小兔不断长大也会生小兔,每次生一公一母,问第十二个月底有多少小兔。
static void Main(string[] args)
{
int tu1 = , tu2 = ;//tu1是倒数第一个月的兔子数,tu2是倒数第二个月的兔子数,斐波那契数列
int tu=;// for (int i = ; i <= ;i++ )
{
tu = tu1 + tu2;
tu2 = tu1;
tu1 = tu;
}
Console.WriteLine(tu);
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class5
{
static void Main(string[] args)
{
int i = ;
int count = ; //记录与7有关的数字的个数
while (i <= )
{
if (i % == || i % == || i / == )
{
Console.Write(i + "\t");
count++;
//
}
i++;
//
}
//
Console.Write("共有" + count + "个与7相关的数");
}
}
}

1.买东西。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 元旦
{
//购物券100元,香皂(5元),牙刷(2元),洗发水(20元)。正好花完,怎么花?
static void Main(string[] args)
{
for (int a=;a<=;a++)
{
for (int b = ; b <= ; b++)
{
for (int c = ; c <= ;c++ )
{
if (*a+*b+*c==)
{
Console.WriteLine("应该买香皂"+a+"块,牙刷"+b+"只,洗发水"+c+"瓶。");
}
}
}
}
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 百钱白鸡
{
//有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡一文钱一只,小鸡半文钱一只。如何买?
static void Main(string[] args)
{
for (int a = ; a <= ;a++ )
{
for (int b = ; b <= ;b++ )
{
for (double c = ; c <= ;c++ )
{
if(a*+b*+c*0.5==&&a+b+c==)
{
Console.WriteLine("公鸡"+a+"只,母鸡"+b+"只,小鸡"+c+"只。");
}
}
}
}
}
}
}

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
问应当让哪几个人去?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class 侦察队
{
static void Main(string[] args)
{
for (int a = ; a <= ;a++ )
{
for (int b = ; b <= ;b++ )
{
for (int c = ; c <= ;c++ )
{
for (int d = ; d <= ;d++ )
{
for (int e = ; e <= ;e++ )
{
for (int f = ; f <= ;f++ )
{
if (a + b >= && a + d <= && a + e + f == && b + c != && c + d == && (d + e == || d == ))
{
Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d+";e="+e+";f="+f);
}
}
}
}
}
} }
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
class Class1
{
//123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
static void fff(string[] args)
{
for (int a = -; a <= ;a=a+ )//-1代表减号,1代表加号
{
for (int b = -; b <= ;b=b+ )
{
for (int c = -; c <= ;c=c+ )
{
for (int d = -; d <= ;d=d+ )
{
if (+a*+*b+*c+*d==)
{
Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d);
}
}
}
}
}
}
}
}

 (三)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.球员打分

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
static void Main(string[] args)
{
int[] a = new int[];
Console.WriteLine("********球员训练记录********"); for (int i=;i<a.Length;i++)
{
Console.Write("请输入第"+(i+)+"个球员的成绩:");
a[i] =Convert.ToInt32( Console.ReadLine());
} for (int m = ; m < a.Length; m++)
{
Console.WriteLine("第"+(m+)+"个球员的成绩是"+a[m]+"。");
} int sum = ;
double ave = ;
for (int n = ; n < a.Length;n++ )
{
sum = sum + a[n]; }
ave = 1.0 * sum / a.Length;
Console.WriteLine("球员总成绩是"+sum+",平均成绩是"+ave+"。"); int max = ,min=;
int maxSub = -, minSub = -;
for (int b = ; b < a.Length;b++ )
{
max = max > a[b] ? max : a[b];
maxSub = b;
}
for (int c = ; c < a.Length; c++)
{
if(min>a[c])
{
min=a[c];
minSub=c;
}
}
Console.WriteLine((maxSub+)+"球员中最高成绩是" + max + ","+(minSub+)+"最差成绩是"+min+"。"); }
}
}

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[];
for(int i=;i<;i++)
{
Console.Write("请第"+(i+)+"位同学投票(0-4):");
int temp = Convert.ToInt32(Console.ReadLine());
if(temp < || temp >)
{
Console.WriteLine("废票");
continue;
}
else
{
vote[temp]++;
}
} //计算最终得票。
int max = , maxSub = ;
for(int i=;i<vote.Length;i++)
{
//把每位候选人的票数显示出来。
Console.WriteLine("第" + (i + ) + "号候选人的票数是:" + vote[i]);
//计算最大值。
if(vote[i] > max)
{
max = vote[i];
maxSub = i;
}
} //显示最终结果。
Console.WriteLine("最终投票结果为:"+(maxSub+)+"号候选人当选,当选票数是"+max+"票。");
}
}
}

3.36选7

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class3
{
static void Main(string[] args)
{
int[] a = new int[]; Random rand = new Random();
for (int i = ; i < a.Length; i++)//7-代表要生成7个不同的数
{
//生成一个随机数
int n = rand.Next();
n++; //查重
bool chong = false;
for (int j = ; j < a.Length;j++ )
{
if (n==a[j])
{
chong = true;
break;
}
}//for
//确定n合不合理
if (chong == false)
{
a[i] = n;
}
else
{
i--;
}
}//for //显示彩票号
for (int k = ; k < a.Length;k++ )
{
Console.Write(a[k]+"\t");
}
}//Main
}
}

3.青歌赛。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
int[] a = new int[];
//亮分
ShuRu(a); //排序
PaiXu(a); //运算求平均
double avg = YunSuan(a); //输出显示
ShuChu(a, avg);
} private static void ShuChu(int[] a, double avg)
{
Console.WriteLine("去掉两个最高分:" + a[] + "和" + a[]);
Console.WriteLine("去掉两个最低分:" + a[a.Length - ] + "和" + a[a.Length - ]);
Console.WriteLine("该选手最终得分为:" + avg);
} private static double YunSuan(int[] a)
{
//求总分
int sum = ;
for (int i = ; i <= a.Length - ; i++)
{
sum += a[i];
}
//求平均
double avg = (1.0 * sum) / (a.Length - );
return avg;
} private static void PaiXu(int[] a)
{
for (int i = ; i <= a.Length - ; i++)
{
for (int j = ; j <= a.Length - i; j++)
{
if (a[j] > a[j - ])
{
int temp = a[j];
a[j] = a[j - ];
a[j - ] = temp;
}
}
}
} private static void ShuRu(int[] a)
{
for (int i = ; i < a.Length; i++)
{
Console.Write("请第" + (i + ) + "号评委亮分:");
a[i] = Convert.ToInt32(Console.ReadLine());
}
}
}
}

二维数组:

二维数组: 是用来声明指定行指定列的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.学生成绩。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
static void Main(string[] args)
{
int[,] a = new int[, ];
//输入
for (int i = ; i < ;i++ )
{
a[i, ] = i + ;
Console.Write("语文:");
a[i, ] = Convert.ToInt32(Console.ReadLine());
Console.Write("数学:");
a[i, ] = Convert.ToInt32(Console.ReadLine());
a[i, ] = a[i, ] + a[i, ];
}
//输出
Console.WriteLine("学号\t语文\t数学\t总分\t");
for (int n = ; n < ;n++ )
{
for (int m = ; m < ;m++ )
{
Console.Write(a[n,m]+"\t");
}
Console.WriteLine();
}
}
}
}

2.推箱子。

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] {,,,,,, };
Console.Write("请输入要找的数:");
int find=Convert.ToInt32(Console.ReadLine()); int top, bottom, mid;//上限下标,下限下标,中间下标
top = ;
bottom = a.Length - ; while (bottom >= top)//只要下限下标还在上限下标的下面,就循环,否则没找到就结束。
{
//算中间下标
mid = (top + bottom) / ;
//取中间的值
int n = a[mid];
if(n<find)
{
top=mid+;//调整上限的下标
}
else if(n>find)
{
bottom = mid - ;//调整下限的下标
}
else
{
Console.WriteLine("找到了,在第"+mid+"下标处找到"+find);
break;
}
}
}
}
}

冒泡排序的思想:相邻两个数进行依次对比,互换。
两层循环,外层循环趟数,内层循环每趟的次数。
趟数: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])
{
互换。
}
}
}

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
static void Main(string[] args)
{
int[] a = new int[] { , , , , , , , };
//冒泡排序。
for(int i=;i<=a.Length-;i++) //趟数
{
for (int j = ; j <= a.Length - i; j++)//次数
{
if(a[j-] > a[j])
{
int t = a[j - ];
a[j - ] = a[j];
a[j] = t;
}
}
} //显示
for(int k=;k<a.Length;k++)
{
Console.WriteLine(a[k]);
}
}
}
}

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

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

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

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

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

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

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

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

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

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

C#语言小结的更多相关文章

  1. C语言小结之结构类型

    C语言小结之结构类型 @刁钻的游戏 (1)枚举型类型enum COLOR {BLACK,RED,BLUE};//声明一种新的数据类型,其值分别为0,1,2但是用BLACK/RED/BLUE代表也可以这 ...

  2. Zabbix 5.0切换中文语言小结

    最近测试Zabbix 5.0,去修改语言时发现不能选择"Chinese(zh_CN)",这个选项在下拉框中是灰色的(无法选择).提示"You are not able t ...

  3. C语言小结

    1.输入输出: char s; printf("Enter a string"); scanf("%s",&s); printf(''Hello,%s& ...

  4. C语言小结之链表

    链表的学习 在数据结构中有一种结构叫做线性表,线性表是储存一个线性数据的表格,本文就简要的介绍一下线性表的构成. 一.线性表的定义定义:由同种类型数据元素构成的有序数列的线性结构长度.表头.表尾Lis ...

  5. ShoneSharp语言(S#)的设计和使用介绍系列(11)—“类”披炫服靓妆化成“表”

    ShoneSharp语言(S#)的设计和使用介绍 系列(11)—“类”披炫服靓妆化成“表” 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/Sh ...

  6. JS基础-第1天

    JavaScript 第一天笔记 学习目标 了解Javascript的作用及其组成 掌握变量的使用,知道变量的作用是存储数据 掌握变量的命名规范 掌握 JavaScript 的 5 种简单数据类型 掌 ...

  7. python 基础部分重点复习整理--从意识那天开始进阶--已结

    pythonic 风格编码 入门python好博客 进阶大纲 有趣的灵魂 老齐的教程 老齐还整理了很多精华 听说 fluent python + pro python 这两本书还不错! 元组三种遍历, ...

  8. The DOT Language

    CSDN新首页上线啦,邀请你来立即体验! 立即体验 博客 学院 下载 更多 登录注册 The DOT Language 翻译 2014年04月15日 11:27:07 标签: EBNF / 语言 / ...

  9. 33.2.NIO

    4.1概述[理解] BIO Blocking IO,阻塞型IO NIO No Blocking IO,非阻塞型IO 阻塞IO的弊端 在等待的过程中,什么事也做不了 非阻塞IO的好处 不需要一直等待,当 ...

随机推荐

  1. popen3

    windows版本popen3函数 整理了下10年08月给 fossil 捐的代码  implementation of function “popen2” on win32——参考自 Creatin ...

  2. IE 下a标签在 position:absolute 后无法点击的问题

    IE 下 a 标签在 position:absolute 后无法点击的问题 条件 : DOCTYPE 为 XHTML. IE 浏览器 现象 : 将 a 的 position 指定为 absolute, ...

  3. Adding Pagination 添加分页

    本文来自: http://www.bbsmvc.com/MVC3Framework/thread-206-1-1.html You can see from Figure 7-16 that all ...

  4. SSCTF-PWN

    前几天比赛的PWN题,简单写了下. PWN400 漏洞是一个数组越界访问造成的任意地址读写.在对数据排序后,对数据进行查询和更新时,可以访问到数组以外一个元素(4个字节). 程序中存在3种数据结构,第 ...

  5. 【floyed】【HDU1217】【Arbitrage】

    题目大意: 给你几种货币,以及几种汇率关系,问是否存在套利的可能? 思路: 初步想法:图上存在一个环的路径上权值相乘大于1.... 再者:该如何找到图上所有环呢.... 好吧 经过鸟神 和 况神的指点 ...

  6. web中通过注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>版本

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...

  7. div+css中常见的浏览器兼容性处理-兼容不同浏览器

    在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! div+cs ...

  8. C#4 for循环 迭代法 穷举法应用

    for()循环. 四要素: 初始条件,循环条件,状态改变,循环体. 执行过程: 初始条件--循环条件--循环体--状态改变--循环条件.... 注意:for的小括号里面分号隔开,for的小括号后不要加 ...

  9. nginx 学习笔记【持续更新...】

    1.如果在安装过程中出现以下错误 需要安装pcre库 解决方案:yum install pcre 2.如果nginx启动提示端口被占用,则停止该端口的服务再启动nginx,一般为httpd服务 解决方 ...

  10. 关于 <video> 的兼容性测试

    测试浏览器 Firefox: 33.0.1 Chrome: 38.0.2125.111 m Safari: 5.1.7 IE: 9 和 10(6,7,8不考虑) 经测试:FF.Chrome.Safar ...