笔记与心得:黑马程序员C#40课基础知识集合(四)
----------------------以下为我在黑马程序员学习期间整理的笔记和心得,期待与您交流! ----------------------
31、
0 计算机中最小的单位叫"位",bit,比特
就把8个二进制位分成一组,那么8个二进制位叫做 字节
Byte B 计算机能够处理的最小的单位
1KB=1024B
1MB=1024KB
1GB=1024MB-gth);
Console2013-09-11 20:49:07
//练习1.从一个整数组中取出最大值和最小值,计算一个整数数组的所有元素的和
int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int max = 0;
int min = 0;
int sum = 0;
for (int i = 0; i < number.Length;i++ )
{
if (number[i] > max)
{
max = number[i];
}
if (number[i] < min)
{
min = number[i];
}
sum += number[i];
}
Console.WriteLine("最大值为{0},最小值为{1},和为{2}", max, min,sum);
Console.ReadKey();
//练习3.将字符串数组输出为|分割的形式,比如"梅西|卡卡|郑大世"(1)
string[] name = { "梅西", "卡卡", "郑大世" };
string str = "";
for (int i = 0; i < name.Length; i++)
{
str = str + name[i] + "|";
}
Console.WriteLine(str);
Console.ReadKey();
(2)
string[] name = { "梅西", "卡卡", "郑大世" };
string str = "";
for (int i = 0; i < name.Length; i++)
{
if (i == name.Length - 1)
{
str = str + name[i];
}
else
{
str = str + name[i] + "|";
}
}
Console.WriteLine(str);
Console.ReadKey();
//练习4.讲一个整数数组的每一个元素进行如下的处理:如果元素是正数则将这个位置的元素的值加1,如果元素是负数则将这个位置的元素的值减1,如果元素是0,则值不变
string[] name = { "梅西", "卡卡", "郑大世" };
for (int i = name.Length-1; i >= 0; i--)
{
Console.WriteLine(name[i]);
}
Console.ReadKey();
//练习5:将一个字符串数组的元素顺序进行反转{"3","a""8","haha"}{"haha","8","a","3"}.第i个和第Length-i-1个进行交换
string[] name = { "梅西", "卡卡", "郑大世" };
string temp;
for (int i = 0; i < name.Length / 2; i++)
{
//交换第i个与第length-i个
temp = name[i];
name[i] = name[name.Length - 1 - i];
name[name.Length - 1 - i] = temp;
}
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadKey();
.ReadKey();
Console.Clear(); 清屏
32、复习作业讲解
2013-09-11 20:49:07
//练习1.从一个整数组中取出最大值和最小值,计算一个整数数组的所有元素的和
int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int max = 0;
int min = 0;
int sum = 0;
for (int i = 0; i < number.Length;i++ )
{
if (number[i] > max)
{
max = number[i];
}
if (number[i] < min)
{
min = number[i];
}
sum += number[i];
}
Console.WriteLine("最大值为{0},最小值为{1},和为{2}", max, min,sum);
Console.ReadKey();
//练习3.将字符串数组输出为|分割的形式,比如"梅西|卡卡|郑大世"(1)
string[] name = { "梅西", "卡卡", "郑大世" };
string str = "";
for (int i = 0; i < name.Length; i++)
{
str = str + name[i] + "|";
}
Console.WriteLine(str);
Console.ReadKey();
(2)
string[] name = { "梅西", "卡卡", "郑大世" };
string str = "";
for (int i = 0; i < name.Length; i++)
{
if (i == name.Length - 1)
{
str = str + name[i];
}
else
{
str = str + name[i] + "|";
}
}
Console.WriteLine(str);
Console.ReadKey();
//练习4.讲一个整数数组的每一个元素进行如下的处理:如果元素是正数则将这个位置的元素的值加1,如果元素是负数则将这个位置的元素的值减1,如果元素是0,则值不变 string[] name = { "梅西", "卡卡", "郑大世" };
for (int i = name.Length-1; i >= 0; i--)
{
Console.WriteLine(name[i]);
}
Console.ReadKey();
//练习5:将一个字符串数组的元素顺序进行反转{"3","a""8","haha"}{"haha","8","a","3"}.第i个和第Length-i-1个进行交换
string[] name = { "梅西", "卡卡", "郑大世" };
string temp;
for (int i = 0; i < name.Length / 2; i++)
{
//交换第i个与第length-i个
temp = name[i];
name[i] = name[name.Length - 1 - i];
name[name.Length - 1 - i] = temp;
}
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadKey();
33、ReadInt
2013-09-12 10:42:47
Console.WriteLine("请输入你的年龄?");
int age = Readint();
Console.WriteLine("你刚刚输入的年龄为"+age);
Console.WriteLine("请输入你是哪一年出生的?");
int year = Readint();
Console.WriteLine("哦!你是{0}年出生的啊!",year);
Console.WriteLine("请输入你们班级有多少人?");
int count = Readint();
Console.WriteLine("你们班有{0}个人.",count);
Console.ReadLine();
}
public static int Readint()
{
int number = 0;
do
{
try
{
number = Convert.ToInt32(Console.ReadLine());
return number;
}
catch
{
Console.WriteLine("输入有误,请重新输入!");
}
}
while (true);
34、冒泡排序
2013-09-12 14:56:33
在一个循环中,第i个元素与第length-1-i个元素进行交换冒泡排序:
让数组中的元素两两比较(第i个与第i+1个比较),经过n(i-1)遍两两比较,数组中的元素能按照我们预期的的规律排序.
要从大到小排序,我们进行两两比较的时候用<
(从大到小用<,从小到大用>)
N个数需要排n-1趟
第t趟比较的次数为:n-t次
i=0 第一趟
i=1 第二趟
趟数:i+1 t=i+1
For(int i = 0;i>n-1;i++)
{
for(int j = 0;j<n-i-1;j++)
{
int c=a;
a=b;
b=c
}
}
int[] score = { 18, 20, 48, 76, 20, 38, 87, 90, 37, 45, 65, 65, 14, 67, 95 };
for (int i = 0; i > score.Length - 1; i++)
{
for (int j = 0; j < score.Length - 1 - i; j++)
{
if (score[j]>score[j+1])
{
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = score[j];
}
}
}
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine(score[i]);
}
Console.ReadKey();
35、方法
2013-09-12 18:46:32
函数就是将一堆代码进行重用的一种机制.函数就是一段代码,这段代码可能有输入的值(参数),可能会返回值.一个函数就像一个专门做这件事的人,我们调用它来做一些事情,它可能需要我们提供一些数据给它,它执行完成后可能会有一些执行结果给我们.要求的数据就叫做参数,返回的执行结果就是返回值.String s = Console.ReadLine()就是一个有返回结果的函数;
Console.WriteLine("hello")就是一个执行参数的函数,只有告诉WriteLine被打印的数据它才知道如何打印;int i=COnvert.Toint32("22")则是一个既有参数又有返回值的函数.
有了函数写代码就像拼积木,C#中的各种各样的技术其实就是通过for、if等这些基础的语法将不同的函数按照一定的逻辑组织起来.
方法
定义:
[访问修饰符][static]返回值类型 方法名()
{
方法体;
}
命名规则:方法名开头大写,参数名开头小写,参数名,变量名要有意义方法的调用,对于静态方法,如果在同一个类中,直接写名字调用就行了
return可以立即退出方法
功能:用来复用代码.当我们在一个程序中反复的写了同样的代码.那一般情况下,我们可以把需要重复写的代码定义在一个方法中,用的时候只需要调用就行了.
定义方法的语句:
[访问修饰符][static]返回值类型 方法名()
{
方法体
}
static void Main(string[] args)
{
ShowUI();
Console.WriteLine("谢谢使用,按任意键退出!");
Console.ReadKey();
}
/// <summary>
/// 用于显示软件主界面的方法
/// </summary>
public static void ShowUI()
{
Console.WriteLine("**********************************");
Console.WriteLine("* 欢 迎 您 使 用 本 软 件 *");
Console.WriteLine("**********************************");
}
36、参数
2013-09-13 19:31:18
注意:1.方法一般要定义在类中
2.如果方法没有返回值,则返回值类型写void
2.如果方法没有参数,()不能省略
方法的调用:如果是静态方法(由static修饰的)则使用类名.方法名().
在类中调用本调的方法,可以只写方法名();
求两个整数的和
static void Main(string[] args)
{
Add(3,6);
Console.ReadKey();
}
public static void Add(int a, int b)
{
Console.WriteLine("a+b={0}",a+b);
}
int.Parse(string) 转类型在方法名后面的括号内定义变量,叫做定义这个方法的参数.这里定义的变量用于接受调用者传过来的数据
注意:如果一个方法一旦有参数,那么调用者就必须传参数,并且传参数的个数与对应位置上的类型必须一致
static void Main(string[] args)
{
int a = 3;
Text(a);
Console.WriteLine(a);
Console.ReadKey();
}
//被调用者
public static void Text(int a)
{
a = a + 3;
Console.WriteLine(a);
}
求两个整数的和
static void Main(string[] args)
{
Add(3,6);
Console.ReadKey();
}
public static void Add(int a, int b)
{
Console.WriteLine("a+b={0}",a+b);
}
37、返回值
2013-09-13 20:12:37
当调用者想访问我们方法中的变量时,可以通过返回值返回.例如:
string s=Console.ReadLine()
int i= Convert.ToInt("22")
为什么方法前面能定义一个变量接受方法的值,是因为方法中使用了返回值只要在方法中返回了值,那么在调用方法是,前面就应该用一个变量来接受方法的返回值
为什么使用返回值?因为变量规定了类型而不再是隐性
注意:一个方法只能有一个返回值
一旦一个方法有返回值,那注在这个方法值中,就必须通过rerurn语句返回一个值,并且这个值要与返回值类型是同样的
语法:
return result;
static void Main(string[] args)
{
Console.WriteLine("你确定要关机?(y/n)");
string s=ReadAnswer();
//在main方法中,我能知道用户输入的是y还是n吗?
if(s=="y")
{
Console.WriteLine("用户正在关机...");
}
else
{
Console.WriteLine("用户还没有关机...");
}
Console.ReadKey();
}
public static string ReadAnswer()
{
string result = "";
do
{
result = Console.ReadLine();
if (result != "y" && result != "n")
{
Console.WriteLine("输入有误,请重新输入");
}
} while (result != "y" && result != "n");
//当方法执行完成后,result中存储的就是用户存储的y或n
return result;
}
两个数求和(规定为int (有返回值)): static void Main(string[] args)
{
Console.WriteLine("请输入第一个数!");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数!");
int b = int.Parse(Console.ReadLine());
int sum = Add(a, b);
Console.WriteLine("平均值为:{0}", sum / 2);
Console.ReadKey();
}
public static int Add(int a, int b)
{
return a + b;
}
返回值查看是否为闰年. static void Main(string[] args)
{
Console.WriteLine("请输入年份!");
int year = Convert.ToInt32(Console.ReadLine());
bool result = LeapYear(year);
if (result)
{
Console.WriteLine("闰年");
}
else
{
Console.WriteLine("不是闰年");
}
Console.ReadKey();
}
public static bool LeapYear(int year)
{
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
return true;
}
else
{
return false;
}
}
38、复习
2013-09-14 12:20:28
一个变量一旦定义在方法外,类的里面,就叫做类的字段,这个变量就可以被本类的所有方法所访问,但是要注意,静态方法只能访问静态字段.static void Main(string[] args)
{
int max = Max(10, 20);
Console.WriteLine(max);
int[] n = { 1, 2, 3, 4, 5, 6 };
int result = Sum(n);
Console.WriteLine(result);
Console.ReadLine();
}
public static int Sum(int[] numbers)
{
int numberSum = 0;
for (int i = 0; i < numbers.Length; i++)
{
numberSum += numbers[i];
}
return numberSum;
}
/// <summary>
/// 本方法用于找出两个数中最大的数
/// </summary>
/// <param name="i1">第一个数</param>
/// <param name="i2">第二个数</param>
/// <returns></returns>
public static int Max(int i1, int i2)
{
if (i1 > i2)
{
return i1;
}
else
{
return i2;
}
}
39、outref
2013-09-14 14:04:20
由const修饰的常量不能定义静态什么叫方法的重载:
一般在同一个类型中,方法名想通,并且方法的参数的个数不同或者对应位置上的类型不同,才能构成方法的重载
实现步骤:
1.在方法的参数类型前面加out,那么传参数的时候,也必须在number钱加out表明这个参数不是传入的,而是i用来传出值的
2.如果参数是以out形式传入的,那么在传入前可以不赋初值.
3.在方法中对于由out修饰的参数,必须赋值,并且必须在使用前赋值
static void Main(string[] args)
{
string s = "123";
int re;
if (int.TryParse(s, out re) == true)
{
Console.WriteLine("能转换成功,转换后的数字为:" + re);
}
else
{
Console.WriteLine("转换失败");
}
Console.ReadKey();
练习:写一个MyTryParse方法,要求用户传入一个字符串,如果这个字符串能转换成int类型,则方法返回ture,并且转换后的int类型数据通过方法的参数传出.如果字符串不能转换成int类型,则方法返回false,那么out传出的参数将没有意思,在方法中随意赋值就行. static void Main(string[] args)
{
string s = "123";
int re;
if (IntTryParse(s, out re))
{
Console.WriteLine("转换成功!" + re);
}
else
{
Console.WriteLine("转换失败!");
}
Console.ReadKey();
}
static bool IntTryParse(string s, out int result)
{
result = 0;
try
{
result = Convert.ToInt32(s);
return true;
}
catch
{
return false;
}
}
写一个方法,计算一个int类型数组中的每一元素的总和,和最大值与最小值?
static void Main(string[] args)
{
int[] nums = { 3, 4, 2, 5, 1, 6 };
int max, min, sum;
sum = compute(nums, out max, out min);
Console.WriteLine("数组的和为{0},最大值为{1},最小值为{2}",sum,max,min);
Console.ReadKey();
}
static int compute(int[] numbers, out int max, out int min)
{
int sum = 0;
max = numbers[0];
min = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
if (numbers[i] > max)
{
max = numbers[i];
}
if (numbers[i] < min)
{
min = numbers[i];
}
}
return sum;
}
out用于传出值,大方法中必须对out修饰的传数进行赋值
ref双向,既可以传入,也可以传出
字啊传参数的过程中,如果参数有out或ref修饰的话,那么改变方法中的参数变量的值,调用者方法中变量的值也会相应改变
40、习题
2013-09-14 22:03:13
重复让用户输入一个数,判断该数是否是质数,输入q结束? static void Main(string[] args)
{
string input = "";
int number = 0;
bool result;
do
{
Console.WriteLine("请输入一个正整数?");
input = Console.ReadLine();
if (input == "q")
{
Console.WriteLine("程序结束!");
break;
}
try
{
number = Convert.ToInt32(input);
result = zhishu(number);
if (result == true)
{
Console.WriteLine("{0}是质数", number);
}
else
{
Console.WriteLine("{0}不是质数", number);
}
}
catch
{
Console.WriteLine("输入的数据有误,请重新输入!");
continue;
}
}
while (true);
Console.ReadKey();
}
/// <summary>
/// 判断一个数是否为质数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool zhishu(int number)
{
for (int i = 2; i < number; i++)
{
//i能不能等于number
if (number % 1 == 0)
{
//上面的条件一旦成立,2到本身-1之间的数就除尽了,所以不是质数,返回false
return false;
}
}
//循环执行完毕,也就说明上面的条件 number % i == 0 都不成立.说明是一个质数
return true;
将一个字符串组输出为|分割的形式,比如"梅西|卡卡|郑大世"(用方法来实现此功能)
public static string ConString(string[]names)
{
string result = "";
for (int i = 0; i < names.Length; i++)
{
if (i == names.Length - 1)
{
result += names[i];
}
else
{
result += names[i] + "|";
}
}
return result;
}
static void Main(string[] args)
{
string[] names = {"张三","李四","王五" };
string str = ConString(names);
Console.WriteLine(str);
Console.ReadKey();
}
更多精彩
