启动 Visual Studio
a、双击图标
b、调出cmd,输入 devenu
1、.Net平台 .Net FrameWork框架 .Net FrameWork框架提供了一个稳定的运行环境,;来保障我们.Net平台正常的运转 2、C#语言 c sharp 编程语言,如果你想要计算机按照你说的去做,你必须说计算机能够听懂的语言。 2000年 any time any place and on any device 任何时间任何地点都能够让用户通过任何设备获取到用户想要的数据 1997 李开复 XXX 乔布斯 windows phone====》安卓===》wp7===》 wp8======>unity3D Unity3D 老鼠---猫 c# JS BOO 3、两种交互模式 C/S:要求客户的电脑上必须要安装一个客户端:qq、360、快播等..... B/S:要求客户的电脑上只需要安装一个浏览器。 4、IDE IDE指的就是我们的开发工具。 5、vs的学习 1)、启动VS a、双击图标 b、调出cmd,输入 devenu 2)、解决方案、项目及类之间的关系 解决方案:公司 项目:部门 类:员工 在视图里面找解决方案资源管理器 3)、Program中的各个组成部分 引用命名空间: 京东----》京东在线商城项目---》顾客类 淘宝----》淘宝在线商城项目---》顾客类 高端霸气公司----》老赵牛X项目---》顾客类 4)、方法或者函数 Main函数是我们程序的主入口,你写的代码如果想要被执行的话,必须写在Main函数当中。 5)、 .sln:解决方案文件,里面包含着整个解决方案的信息,可以双击运行。 .csproj:项目文件,里面包含着这个项目的信息,可以双击运行。 6、书写代码需要注意的地方: 1、代码中出现的所有标点都是英文半角 shift键快速切换中文半角和英文半角 shift+空格 切换全角/半角 2、在c#代码中,每行代码的结束,我们都以分号结束,注意:这个分号也是英文半角的分号。 3、Console.WriteLine("要打印的内容"); Console.ReadKey(); 1)、暂停当前程序,等待用户按下任意键继续,按下的任意键将显示在我们的控制台当中
打印 hello world
using System; namespace my_first_demo { class Program { /// <summary> /// 这个方法是判断两个整数中的最大值 /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadKey(); Console.WriteLine("Hello World!"); Console.ReadKey(); } public static int GeyMax(int n1, int n2) { return n1 > n2 ? n1 : n2; } } } public class Person {/// <summary> /// 这个类用来描述人的信息 /// </summary> public string Name { get; set; } public int Age { get; set; } public int Gender { get; set; } }
快捷键操作
运行方式 > F5
变量
using System; namespace _03_变量 { class Program { static void Main(string[] args) { // 官方语言: 声明或者定义了一个int类型的变量 int number; //在内存里开辟了一块能存储整数的空间. // 官方语言: 给这个变量进行赋值 number = 100; // 表示把100存储到了这块空间内 Console.WriteLine(number); // 简写形式 int n = 100; Console.WriteLine(n); double d = 3.14; // 表示小数, 也能存整数 Console.WriteLine(d); string name = "Irving"; // 表示字符串, 必须用双引号 string s = ""; Console.WriteLine(name); Console.WriteLine(s); char gender = '男'; // 表示字符 字符是单引号 Console.WriteLine(gender); decimal money = 1000m; Console.WriteLine(money); } } }
字符串拼接
string name = "Irving"; int age = 18; char gender = '男'; Console.WriteLine("我是{0},年龄{1}","Irving",18); // 我是Irving,年龄18 Console.WriteLine("我是{0},年龄{1}",name,age, gender); // 我是Irving,年龄18
变量交换
//int n1 = 10; //int n2 = 20; //Console.WriteLine("{0},{1}", n1, n2); //int n1 = 10; //int n2 = 20; //int tmp = n1; //n1 = n2; //n2 = tmp; //Console.WriteLine("{0},{1}", n1, n2); int n1 = 10; int n2 = 20; n1 = n1 - n2; // -10 n2 = n1 + n2; // 10 n1 = n2 - n1; // 20
控制台接收用户输入
Console.WriteLine("请输入用户名"); name = Console.ReadLine(); Console.WriteLine("your name is {0}", name);
转义符
// '/' 这是转义符 Console.WriteLine("今天天气真好 我想出门静静"); Console.WriteLine("今天天气真好"我想出门静静"");
输出到文件
string s = "今天天气真 好我想出门静静"; System.IO.File.WriteAllText(@"E: mpa.txt", s);
保留原格式输出 ”@“
string s; s = "今天天气真 好我想出门静静"; s = @"今天天气真 好我想出门静静"; string path; path = "E:\tmp\a.txt"; path = @"E: mpa.txt"; System.IO.File.WriteAllText(@"E: mpa.txt", s);
运算符
int seconds = 107653; int days = seconds / 86400; // 求出天数 seconds = seconds % 86400; // 求出剩余秒数 int hours = seconds / 3600; // 求出小时 seconds = seconds % 3600; int mins = seconds / 60; seconds = seconds % 60; Console.WriteLine("{0}天 {1}小时 {2}分钟 {3}秒", days, hours, mins, seconds);
类型转换 Convert
string s = "123"; Console.WriteLine(Convert.ToInt32(s)); double d= Convert.ToDouble(s); Console.WriteLine(d);
if 分支
Console.WriteLine(">>>请输入数字"); int age = Convert.ToInt32(Console.ReadLine()); if (age < 18) { Console.WriteLine("你还未成年"); } else if (age > 30 && age < 100) { Console.WriteLine("你大于30岁"); } else { Console.WriteLine("年龄不正确"); }
异常捕获
Console.WriteLine("请输入数字"); int num=0; bool flag=true; try { num = Convert.ToInt32(Console.ReadLine()); } catch { Console.WriteLine("输入的不正确"); flag = false; } if (flag) { Console.WriteLine(num * 2); }
switch 多条件的定值判断
Console.WriteLine("请输入李四的年终评定!"); string level = Console.ReadLine(); int salary = 500; switch (level) { case "A": salary += 500; break; case "B": salary += 300; break; default: Console.WriteLine("输入有误程序退出"); break; } Console.WriteLine(salary);
while 循环
int count = 0; int sum = 0; while (count <= 100) { Console.WriteLine(count); if (count % 2 == 0) { sum += count; } ++count; } Console.WriteLine(sum);
for循环
class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); for (int i = 0; i < 10; i++) { Console.WriteLine(i); } } }
for循环 乘法表
static void Main(string[] args) { Console.WriteLine("Hello World!"); for (int i = 1; i < 10; i++) { Console.WriteLine(); for (int j = 1; j < 10; j++) { if (i>=j) { Console.Write("{0}*{1}={2} ", j, i, i * j); } } } }
常量
常量不能被赋值修改
const int n = 20;
枚举
using System; namespace _12_枚举类型 { // 将枚举声明到命名空间的下面,类的外面,表示这个命名空间下,所有的类都可以使用这个枚举。 public enum Gender { 男, 女 } public enum QQState { OnLine, OffLine, Leave, Busy, QMe } class Program { static void Main(string[] args) { Gender gender12 = Gender.男; // 使用枚举并赋值 Console.WriteLine(gender12); Console.WriteLine(12); QQState state = QQState.OnLine; Console.WriteLine(state); int n = (int)state; // 强转int类型 Console.WriteLine(n); // 输出结果为0 int n1 = 3; QQState state1 = (QQState)3; // int转枚举 Console.WriteLine(state1); // 输出枚举 string s = state1.ToString(); // 枚举转字符串 Console.WriteLine(s); QQState state2 = (QQState)Enum.Parse(typeof(QQState), "0"); // 字符串转枚举 Console.WriteLine(state2); } } }
结构
namespace _13_结构 { public struct Person { public string _name; public int _age; public Gender _gender; } public enum Gender { 男, 女 } class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Person zsPerson; zsPerson._name = "张三"; zsPerson._age = 20; zsPerson._gender = Gender.男; Console.WriteLine(zsPerson._name); } } }
数组
声明数组
namespace _14_数组 { class Program { static void Main(string[] args) { // 声明数组,并存进去10个变量 int[] nums = new int[10]; int[] numsTwo = {1,2,3,4,5,6 }; int[] numsThree = new int[3] { 1,2,3 }; int[] numsFour = new int[] { 1,2,3,4,5,6 }; string[] nums2 = new string[10]; bool[] nums3 = new bool[10]; for (int i = 0; i < nums.Length; i++) { nums[i] = i; } Console.WriteLine(nums); Console.WriteLine(numsTwo); Console.WriteLine(numsThree); Console.WriteLine(numsFour); Console.WriteLine(nums2); Console.WriteLine(nums3); } } }
数组 冒泡 排序
int[] nums = { 1, 7, 3, 9, 2, 6, 5, 4 }; for (int i = 0; i < nums.Length -1; i++) { for (int j = 0; j < nums.Length -1 -i; j++) { if (nums[j + 1] < nums[j]) { int tmp = nums[j]; nums[j] = nums[j+1]; nums[j + 1] = tmp; } } } for (int i = 0,length = nums.Length; i < length; i++) { Console.WriteLine(nums[i]); }
方法(函数)
using System; namespace _16_方法 { class Program { static void Main(string[] args) { Console.WriteLine(Program.GetMax(3, 4)); } /// <summary> /// 计算2个数的最大值,并返回 /// </summary> /// <param name="n1">第一个整数</param> /// <param name="n2">第二个整数</param> /// <returns></returns> public static int GetMax(int n1, int n2) { return n1 < n2? n2:n1; return n1 * n2; } } }
函数调用细节
using System; namespace _16_方法 { class Program { public static int _number = 10; static void Main(string[] args) { Console.WriteLine(Program.GetMax(3, 4)); int b = 10; int a = 5; int res = Test(a); Console.WriteLine(res); Console.WriteLine(_number); } /// <summary> /// 计算2个数的最大值,并返回 /// </summary> /// <param name="n1">第一个整数</param> /// <param name="n2">第二个整数</param> /// <returns></returns> public static int GetMax(int n1, int n2) { return n1 < n2? n2:n1; return n1 * n2; } public static int Test(int a) { a = a + 3; return a; Console.WriteLine(_number); } public static void TestTwo(int a) { a = a + 3; Console.WriteLine(_number); } } }
方法 out 关键字的使用
示例一
using System; namespace _16_方法 { class Program { public static int _number = 10; static void Main(string[] args) { int[] nums = { 1, 2, 4, 7, 8, 5, 47, 8, 9, 6, 2 }; int[] res = GetMaxMinSumAvg(nums); for (int i = 0,length = res.Length; i < length; i++) { Console.Write(res[i]+" "); } Console.WriteLine(); int max, min, sum; double avg; GetMaxMinSumAvgTwo(nums, out max, out min, out sum, out avg); Console.Write(max +" "+ min + " " + sum + " "+ avg + " "); Console.WriteLine(); } /// <summary> /// 用数组返回多个相同类型的值 /// </summary> /// <param name="nums"></param> /// <returns></returns> public static int[] GetMaxMinSumAvg(int[] nums) { int max = int.MinValue; int min = int.MaxValue; int sum = 0; for (int i = 0,length=nums.Length; i < length; i++) { max = nums[i] > max ? nums[i] : max; min = nums[i] < min ? nums[i] : min; sum += nums[i]; } int avg = sum/ nums.Length; int[] res ={ max,min,sum,avg}; return res; } /// <summary> /// 用out返回多个不同类型的值 /// </summary> /// <param name="nums"></param> /// <param name="max"></param> /// <param name="min"></param> /// <param name="sum"></param> /// <param name="avg"></param> public static void GetMaxMinSumAvgTwo(int[] nums, out int max,out int min, out int sum, out double avg) { max = int.MinValue; min = int.MaxValue; sum = 0; for (int i = 0, length = nums.Length; i < length; i++) { max = nums[i] > max ? nums[i] : max; min = nums[i] < min ? nums[i] : min; sum += nums[i]; } // avg = sum / nums.Length; avg = 3.6; } } }
示例二
using System; namespace _16_方法 { class Program { static void Main(string[] args) { Console.WriteLine("请输入用户名"); string username = Console.ReadLine(); Console.WriteLine("请输入密码"); string password = Console.ReadLine(); string msg; bool isLogin = IsLogin(username, password, out msg); Console.WriteLine(isLogin); } public static bool IsLogin(string name, string pwd, out string msg) { if (name == "admin" && pwd == "123") { msg = "login success"; return true; } msg = "login wrong"; return false; } } }
方法 ref 的使用
using System; namespace _17_方法ref的使用 { class Program { static void Main(string[] args) { // 将一个变量作为参数带入到方法中进行改变,改变完成后再传递回来,变量的指向是同一个地方 double price = 5000; addition(ref price); // 传入带 ref 的参数 Console.WriteLine(price); int n1 = 3, n2 = 4; Console.WriteLine("{0} {1}", n1, n2); exchange(ref n1, ref n2); Console.WriteLine("{0} {1}",n1,n2); } public static void addition(ref double s) // 接收带 ref 的参数 { s += 500; } public static void subtract(double s) { s += 500; } public static void exchange(ref int n1, ref int n2) { int tmp = n1; n1 = n2; n2 = tmp; } } }
方法中的 params 参数
using System; namespace _18_方法_params_的使用 { class Program { static void Main(string[] args) { // int[] scores = { 1, 2, 3, 6, 58,56, 4, 58, 464, 49,30 }; Console.WriteLine(TotalScore("Irving",99, 1, 2, 3, 6, 58, 56, 4, 58, 464, 49, 30)); } public static String TotalScore(string name,int num, params int[] scores) { int totalScore = 0; for (int i = 0,length=scores.Length; i < length; i++) { totalScore += scores[i]; } return String.Format("{0}学号{2}的总分数是:{1}",name, totalScore,num); } } }
方法的重载
using System; namespace _19_方法的重载 { class Program { static void Main(string[] args) { // 如果参数的类型相同,个数就不能相同 // 如果参数的个数相同,类型就不能相同 M("",""); } public static void M(int n1, int n2) { int result = n1 + n2; } public static double M(double d1, double d2) { return d1 + d2; } public static void M(int n1, int n2, int n3) { int result = n1 + n2 + n3; } public static string M(string s1,string s2) { return s1 + s2; } } }
方法 递归
using System; namespace _20_方法的递归 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); TellStory(); } public static int i = 0; public static void TellStory() { Console.WriteLine("从前有座山"); Console.WriteLine("山里有做庙"); Console.WriteLine(i); if (i>10) { return; // 递归的结束条件 } i++; TellStory(); // 递归 自己调用自己 } } }
飞行棋
using System; namespace _21_飞行棋 { class Program { public static int[] Maps = new int[100]; public static int[] PlayerPos = { 0,0}; public static string[] PlayerNames = new string[2]; static void Main(string[] args) { Console.Clear(); GameShow(); Console.WriteLine("{0}的士兵是A",PlayerNames[0]); Console.WriteLine("{0}的士兵是B",PlayerNames[1]); InitMap(); DrowMap(); while (PlayerPos[0]<99 && PlayerPos[1]<99) { PlayGame(0); PlayGame(1); } Win(); } /// <summary> /// 画游戏头 /// </summary> public static void GameShow() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("***********飞行旗*********"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("**************************"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("请输入玩家A的姓名"); PlayerNames[0] = Console.ReadLine(); Console.WriteLine("请输入玩家B的姓名"); PlayerNames[1] = Console.ReadLine(); CheckName(); } /// <summary> /// 初始化地图 /// </summary> public static void InitMap() { int[] luckyturn = { 23, 40, 55, 69, 83 };// 幸运轮盘◎ for (int i = 0,length=luckyturn.Length; i < length; i++) { Maps[luckyturn[i]] = 1; } int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ for (int i = 0, length = landMine.Length; i < length; i++) { Maps[landMine[i]] = 2; } int[] pause = { 4, 9, 27, 60, 93, 2, 3, 7, 8 };//暂停▲ for (int i = 0, length = pause.Length; i < length; i++) { Maps[pause[i]] = 3; } int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐 for (int i = 0, length = timeTunnel.Length; i < length; i++) { Maps[timeTunnel[i]] = 4; } } public static void DrowMap() { Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐"); // 第一行 for (int i = 0, length = 30; i < length; i++) { GameLattice(i); } Console.WriteLine(); // 左边竖行 for (int i = 30, length=35; i < length; i++) { for (int j = 0; j < 29; j++) { Console.Write(" "); } GameLattice(i); Console.WriteLine(); } // 第二行 for (int i = 64, length=35; i > length; i--) { GameLattice(i); } // 右边竖行 for (int i = 64, length = 70; i < length; i++) { GameLattice(i); Console.WriteLine(); } // 第三行 for (int i = 70, length = 99; i < length; i++) { GameLattice(i); } Console.WriteLine(); } public static void GameLattice(int i) { if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i) { Console.Write("<>"); } else if (PlayerPos[0] == i) { Console.Write("A"); } else if (PlayerPos[1] == i) { Console.Write("B"); } else { switch (Maps[i]) { case 0: Console.ForegroundColor = ConsoleColor.White; Console.Write("□"); break; case 1: Console.ForegroundColor = ConsoleColor.Red; Console.Write("◎"); Console.ForegroundColor = ConsoleColor.White; break; case 2: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("☆"); Console.ForegroundColor = ConsoleColor.White; break; case 3: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("▲"); Console.ForegroundColor = ConsoleColor.White; break; case 4: Console.ForegroundColor = ConsoleColor.Green; Console.Write("卐"); Console.ForegroundColor = ConsoleColor.White; break; } } } public static void CheckName() { while (true) { if (PlayerNames[0] == "") { Console.WriteLine("输入的玩家A不能为空"); PlayerNames[0] = Console.ReadLine(); } else if (PlayerNames[1] == "") { Console.WriteLine("输入的玩家B不能为空"); PlayerNames[1] = Console.ReadLine(); } else if (PlayerNames[0] == PlayerNames[1]) { Console.WriteLine("玩家B的名字不能和玩家A为相同,请重新输入玩家B"); PlayerNames[1] = Console.ReadLine(); } else { break; } } } public static void RestSys() { for (int i = 0,length = PlayerPos.Length ; i < length; i++) { PlayerPos[i] = PlayerPos[i] < 0 ? 0 : PlayerPos[i]; PlayerPos[i] = PlayerPos[i] > 99 ? 99 : PlayerPos[i]; } Console.Clear(); DrowMap(); } public static void PlayGame(int playerIndex) { int playerOther = 1; if (playerIndex == 1) { playerOther = 0; } int dice = 0; Console.WriteLine("{0} 按任意键开始掷色子", PlayerNames[playerIndex]); Console.ReadLine(); dice = 4; Console.WriteLine("{0} 掷出了{1}", PlayerNames[playerIndex],dice); PlayerPos[playerIndex] += dice; // 玩家踩到了另一个玩家 RestSys(); if (PlayerPos[0]==PlayerPos[1]) { PlayerPos[playerOther] -= 6; if (PlayerPos[playerOther] < 0) { PlayerPos[playerOther] = 0; } } else { // 踩到了关卡 switch (Maps[PlayerPos[playerIndex]]) { case 0: break; case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘 1:和另一个玩家交换位置 2: 让另一个玩家后退6格",PlayerNames[playerIndex]); String input; while (true) { input = Console.ReadLine(); if (input == "1") { int tmp = PlayerPos[0]; PlayerPos[0] = PlayerPos[1]; PlayerPos[1] = tmp; } else if (input == "2") { PlayerPos[playerOther] = PlayerPos[playerOther] - 6; } else { continue; } RestSys(); break; } Console.ReadKey(); break; case 2: PlayerPos[playerIndex] -= 6; RestSys(); Console.WriteLine("玩家{0}踩到了地雷后退6格", PlayerNames[playerIndex]); break; case 3: RestSys(); Console.WriteLine("玩家{0}踩到了暂停一回合", PlayerNames[playerIndex]); PlayGame(playerOther); break; case 4: PlayerPos[playerIndex] += 10; RestSys(); Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerIndex]); break; } } } public static void Win() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" ◆ "); Console.WriteLine(" ■ ◆ ■ ■"); Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■"); Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■"); Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■"); Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■"); Console.WriteLine(" ■ ■ ■ ■ ■ ■ "); Console.WriteLine(" ■ ■ ■ ■ ● ■ "); Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●"); Console.ResetColor(); } } }