C#阶段小结
一、数据类型:
(一)内建类型:
整型(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,Day Of Week, Day Of Year
AddYears(n),AddMonths(),AddDays().........
ToString("格式化字符串"):格式显示。
yyyy,yy——年份。MM,M——月份。dd,d——天。hh,h——时。mm,m——分。ss,s——秒。ms——毫秒
(三)自定义类型
struct
二、变量与常量:
(一)变量就是装数据容器。——U盘
定义:
数据类型 变量名[ = 值],变量名[ = 值],....;
int a,b; int a = 5,b;
变量的命名规则:
1.变量名只能由字母、数字、下划线组成
2.只能字母,下划线开头
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个
+ - * / % ++ --
整数除整数还是整数。
(二)关系——6个
== != > >= < <=
(三)逻辑——3个
&& || !
(四)其它
1.复合运算符:+= -= *= /= %=
2.赋值: =
3.条件运算符:表达式1?表达式2:表达式3
四、语句:
顺序、分支、循环
(一)分支——if
if(表达式)
{
}
if(表达式)
{
}
else
{
}
if(表达式)
{
}
else if(表达式)
{
}
...
else
{
}
if(表达式)
{ if(表达式)
{
}
else
{
}
}
else
{
...
}
例:
第一题:判断闰年平年
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 % 400 == 0) { Console.WriteLine(year + "年是闰年"); } else if (year % 4 == 0 && year % 100 != 0) { Console.WriteLine(year + "年是闰年"); } else { Console.WriteLine(year + "年是平年"); } } } }
第二题:判断一元二次方程根的情况
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 == 0) { Console.WriteLine("不是一元二次方程"); } else { int d = b * b - 4 * a * c; if (d < 0) { Console.WriteLine("一元二次方程无实根"); } else if (d == 0) { Console.WriteLine("一元二次方程有两个相同实根"); } else { Console.WriteLine("一元二次方程有两个不同实"); } } } } }
第三题:判断男女的身高体重是否正常
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 - 100 + 3) { Console.WriteLine("你的体重偏胖。"); } else if (t < high - 100 - 3) { Console.WriteLine("你的体重偏瘦。"); } else if (t >=high - 100 -3&& t<= high -100 +3) { Console.WriteLine("你的体重正常"); } } else if (sex == "女") { if (t > high - 110 + 3) { Console.WriteLine("你的体重偏胖。"); } else if (t < high - 110 - 3) { Console.WriteLine("你的体重偏瘦。"); } else { Console.WriteLine("你的体重正常"); } } else { Console.WriteLine("输入的不正确,请核实。"); } } } }
第四题:输入一个日期判断是否正确
static void Main(string[] args) { int year = 0, month = 0, day = 0; //输入 Console.Write("请输入年:"); year = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入月:"); month = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入日:"); day = Convert.ToInt32(Console.ReadLine()); //判断运算输出 //判断年份是否正确 if(year < 0 || year>9999) { Console.WriteLine("年输入不正确"); } else { Console.WriteLine("年正确"); } //判断月份是否正确 if (month < 1 || month > 12) { Console.WriteLine("月输入不正确"); } else { Console.WriteLine("月正确"); } //判断天是否正确 if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { if(day < 1 || day>31) { Console.WriteLine("天输入错误,大月份最多是31天"); } else { Console.WriteLine("天正确"); } } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (day < 1 || day > 30) { Console.WriteLine("天输入错误,小月份最多是30天"); } else { Console.WriteLine("天正确"); } } else if(month == 2) { //闰年与平年的判断 if(year%400==0 || year%4==0&&year%100!=0) { //闰年 if (day < 1 || day > 29) { Console.WriteLine("天输入错误,闰年2月份最多是29天"); } else { Console.WriteLine("天正确"); } } else { //平年 if (day < 1 || day > 28) { Console.WriteLine("天输入错误,平年2月份最多是28天"); } else { Console.WriteLine("天正确"); } } } } } }
(二)循环
for(初始条件;循环条件;变量改变)
{
循环体
}
循环的四要素:
循环的嵌套:打印星号。
两类问题:迭代法,穷举法。
迭代法: 按照某种规律通过循环迭代求解。
第一题:求100以内所有数的和。
第二题:求阶乘!(6的阶乘)
第三题:求年龄。(有6个小孩子排在一起,问第一个多大年龄,她说:比第二个小2岁,问第二个多大年龄,她说:比第三个小2岁,以此类推,问第六个多大年龄,她说:自己16岁,问第一个小孩子几岁?
第四题:一张纸的厚度是:0.15mm,假设这张纸足够大可以无限次对折,问:折26次的高度是多少?
第五题:棋盘放粮食(请在第一个格子放1粒粮食,第二个格子放2粒,第三个格子放四粒,以此类推,每个格子放的粮食都是前一个格子里粮食的2倍,直到放到第64个格子,请问第64个格子需要多少粮食?假如1粒粮食重量0.02克,那么棋盘64个格子的粮食一共重多少千克?
第六题:公园里有一只猴子和一堆桃子,猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的,到了第七天,猴子睁开眼发现只剩下一个桃子了,问公园原来有多少桃子?
第七题:落球问题(一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起后的高度是多少?)
第八题:兔子小兔子的问题(一对新生兔,到三个月开始生一对小兔,以后每个月都会生一对小兔,小兔不断长大也会生小兔。假设兔子不死,每次只能生一对(公母),问第24个月末有多少只兔子?)
二、穷举法: 用循环把各种可能的情况都给走一遍,然后用if条件把满足要求的结果给筛选出来。
第一题:找出100以内的与7有关的数。
第二题:有三种硬币若干:1分,2分,5分。要组合1毛5,有哪些组合方式?
第三题:买东西。小张过元旦发了100元的购物券,他要买香皂(5元),牙刷(2元),洗发水(20元)。要想把100元正好花完,如何买这三样东西?
第四题:百鸡百钱。有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只。如何买?
第五题:百马百石。有100石粮食,母匹大马驮2石,每匹中马驮1石,每两匹小马驹一起驮1石。要用100匹马驮完100石粮食,如何按排?
第六题:某侦察队接到一项紧急任务,要求在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
问应当让哪几个人去?
第七题:123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
五、数组:
思想:解决大量同类数据存储和操作的问题。
特点:连续,同一类数据。
分类:一维数组,二维数组,多维数组。
一维数组:
定义: 数据类型[] 数组名 = new 数据类型[数组的长度] [{初始化}];
赋值: 数组名[下标] = 值;
可以与循环结合起来。
取值: 数组名[下标];
可以与循环结合起来。
第一题:做一个教练为6位球员打分的程序。并在此基础上显示球员总分和平均分,最高分和最低分,以及球员的代号。
class Class2 { static void Main(string[] args) { int[] a = new int[6]; Console.WriteLine("********球员训练记录********"); for (int i=0;i<a.Length;i++) { Console.Write("请输入第"+(i+1)+"个球员的成绩:"); a[i] =Convert.ToInt32( Console.ReadLine()); } for (int m = 0; m < a.Length; m++) { Console.WriteLine("第"+(m+1)+"个球员的成绩是"+a[m]+"。"); } int sum = 0; double ave = 0; for (int n = 0; n < a.Length;n++ ) { sum = sum + a[n]; } ave = 1.0 * sum / a.Length; Console.WriteLine("球员总成绩是"+sum+",平均成绩是"+ave+"。"); int max = 0,min=200; int maxSub = -1, minSub = -1; for (int b = 0; b < a.Length;b++ ) { max = max > a[b] ? max : a[b]; maxSub = b; } for (int c = 0; c < a.Length; c++) { if(min>a[c]) { min=a[c]; minSub=c; } } Console.WriteLine((maxSub+1)+"球员中最高成绩是" + max + ","+(minSub+1)+"最差成绩是"+min+"。"); } } }
第二题:30个人投票,从5个人中选出一个班长
static void Main(string[] args) { //30人投票,从5个候选人中选一个出来 int[] vote = new int[5]; for (int i = 0; i < 30; i++) { Console.WriteLine("请第" + (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+"票");<br> }
第三题:制作一个36选7的彩票生成器
static void Main(string[] args) { int[] a = new int[7]; Random rand = new Random(); for (int i = 0; i < 7; i++) //7--代表要生成7个不同的数 { //生成一个随机数。 int n = rand.Next(36); n++; //查重 bool chong = false; for(int j=0;j<a.Length;j++) { if(n == a[j]) { chong = true; break; } } //才能确定n合不合理 if(chong == false) { a[i] = n; } else { i--; }//if }//for //显示彩票号码 for(int k = 0;k<a.Length;k++) { Console.Write(a[k] + "\t"); } }//main } }
第五题:青歌赛中有10个评委给一个选手打分,每打分后,要去掉一个最高分和一个最低分,计算该选手的平均得分。
static void Main(string[] args) { int[] a = new int[10]; //亮分 ShuRu(a); //排序 PaiXu(a); //运算求平均 double avg = YunSuan(a); //输出显示 ShuChu(a, avg); } private static void ShuChu(int[] a, double avg) { Console.WriteLine("去掉两个最高分:" + a[0] + "和" + a[1]); Console.WriteLine("去掉两个最低分:" + a[a.Length - 1] + "和" + a[a.Length - 2]); Console.WriteLine("该选手最终得分为:" + avg); } private static double YunSuan(int[] a) { //求总分 int sum = 0; for (int i = 2; i <= a.Length - 3; i++) { sum += a[i]; } //求平均 double avg = (1.0 * sum) / (a.Length - 4); return avg; } private static void PaiXu(int[] a) { for (int i = 1; i <= a.Length - 1; i++) { for (int j = 1; j <= a.Length - i; j++) { if (a[j] > a[j - 1]) { int temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; } } } } private static void ShuRu(int[] a) { for (int i = 0; i < a.Length; i++) { Console.Write("请第" + (i + 1) + "号评委亮分:"); a[i] = Convert.ToInt32(Console.ReadLine()); } } } }
二维数组:
定义:
数据类型[,] 数组名 = new 数组类型[行数,列数] [{初始化}];
赋值:
数组名[行下标,列下标] = 值;
取值:
数组名[行下标,列下标];
第一题:输入学生的成绩
static void Main(string[] args) { int[,] a = new int[3, 4]; //输入 for (int i = 0; i < 3;i++ ) { a[i, 0] = i + 1; Console.Write("语文:"); a[i, 1] = Convert.ToInt32(Console.ReadLine()); Console.Write("数学:"); a[i, 2] = Convert.ToInt32(Console.ReadLine()); a[i, 3] = a[i, 1] + a[i, 2]; } //输出 Console.WriteLine("学号\t语文\t数学\t总分\t"); for (int n = 0; n < 3;n++ ) { for (int m = 0; m < 4;m++ ) { Console.Write(a[n,m]+"\t"); } Console.WriteLine(); } } } }
第二题:推箱子游戏
int x=3, y=1;//记录小人初始位置 int[,] map= new int[10, 10] { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,4,2,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,0,0,0,0,1}, {1,0,0,0,0,0,0,0,3,1}, {1,1,1,1,1,1,1,1,1,1} }; //打印初始图 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 0) { Console.Write(" "); } else if (map[i, j] == 1) { Console.Write("■"); } else if (map[i, j] == 2) { Console.Write("□"); } else if (map[i, j] == 3) { Console.Write("★"); } else if (map[i, j] == 4) { Console.Write("♀"); } } Console.WriteLine(); } //在键盘接受指令,对指令分析运算,输出数据 while (true)//无数次执行循环体 { ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令 int t = map[x,y]; if (s.Key.ToString() == "RightArrow")//若接受指令为“→”, { if (map[x, y + 1] == 0)//若右边方格为空格,小人物与空格交换数据 { map[x, y] = map[x, y + 1]; map[x, y + 1] = t; y++; } else if (map[x, y + 1] == 2 && map[x, y + 2] !=1)//若右边方格为箱子,右边方格接受小人物数据,小人物方格数据变零,右数第二个方格接受箱子方格数据 { int m = map[x, y + 1]; map[x, y + 1] = t; map[x, y] = 0; map[x, y + 2] = m; y++; } } else if (s.Key.ToString() == "LeftArrow") { if (map[x, y - 1] == 0) { map[x, y] = map[x, y - 1]; map[x, y - 1] = t; y--; } else if (map[x, y - 1] == 2 && map[x, y - 2]!=1) { int m = map[x, y - 1]; map[x, y - 1] = t; map[x, y] = 0; map[x, y - 2] = m; y--; } } else if (s.Key.ToString() == "UpArrow") { if (map[x - 1, y]==0) { map[x, y] = map[x - 1, y]; map[x-1,y] = t; x--; } else if (map[x - 1, y] == 2 && map[x-2, y ] != 1) { int m = map[x-1, y ]; map[x - 1, y] = t; map[x, y] = 0; map[x - 2, y] = m; x--; } } else if (s.Key.ToString() == "DownArrow") { if (map[x + 1, y] == 0) { map[x, y] = map[x + 1, y]; map[x + 1, y] = t; x++; } else if (map[x + 1, y] == 2 && map[x + 2, y] != 1) { int m = map[x + 1, y]; map[x + 1, y] = t; map[x , y] = 0; map[x + 2, y] =m; x++; } } Console.Clear(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 0) { Console.Write(" "); } else if (map[i, j] == 1) { Console.Write("■"); } else if (map[i, j] == 2) { Console.Write("□"); } else if (map[i, j] == 3) { Console.Write("★"); } else if (map[i, j] == 4) { Console.Write("♀"); } } Console.WriteLine(); } if (map[9, 9] == 2) Console.WriteLine("过关!"); }
两个应用:二分法查找,冒泡排序。
二分法查找思想:前提是数组有序,每次找中间的值对比,否满足条件就扔一半。
使用最大下标max、最小下标min,中间值下标mid,控制查找的范围。 mid = (max+min)/2; max = mid+1; min = mid-1;
如果一直查到min>max就结束了,说明没有找到。
static void Main(string[] args) { int[] a = new int[] {3,5,7,9,13,14,18 }; Console.Write("请输入要找的数:"); int find=Convert.ToInt32(Console.ReadLine()); int top, bottom, mid;//上限下标,下限下标,中间下标 top = 0; bottom = a.Length - 1; while (bottom >= top)//只要下限下标还在上限下标的下面,就循环,否则没找到就结束。 { //算中间下标 mid = (top + bottom) / 2; //取中间的值 int n = a[mid]; if(n<find) { top=mid+1;//调整上限的下标 } else if(n>find) { bottom = mid - 1;//调整下限的下标 } 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[8] { 9, 12, 7, 5, 15, 2, 1, 8 }; //冒泡排序。 for(int i=1;i<=a.Length-1;i++) //趟数 { for (int j = 1; j <= a.Length - i; j++)//次数 { if(a[j-1] > a[j]) { int t = a[j - 1]; a[j - 1] = a[j]; a[j] = t; } } } //显示 for(int k=0;k<a.Length;k++) { Console.WriteLine(a[k]); } } } }
六、函数:
什么是函数:能够完成某个独立功能模块就可称之为函数。
为什么要用函数:结构清晰,分工开发,代码重用。
四要素:函数名,形参,返回类型,函数体。
定义语法:
返回类型 函数名(形参列表)
{
函数体
}
调用:
函数名(实参列表);
数据类型 变量名 = 函数名(实参列表);
函数的传值与传址的问题。
1.内建类型,日期时间都是默认传值。 ——ref
2.数组,字符串默认都是传址。
static void Mai3n(string[] args) { int[] m = new int[4] { 3, 5, 7, 9 }; //打印显示数组的初始值 for(int i=0;i<m.Length;i++) { Console.Write(m[i] + "\t"); } //调用函数,传递整个数组 Add(m);//传得是数组名,是传址 Add9(m[2]); //传得是数组的某个元素,是传值。 //打印显示数组的改变后的值 for (int i = 0; i < m.Length; i++) { Console.Write(m[i] + "\t"); } } static void Add9(int a) { Console.WriteLine("Add9被用了,在Add9中把传进来的" + a + "变成了" + a * 100); a = a * 100; } static void Add(int[] a) { for(int i=0;i<a.Length;i++) { a[i] *= 10; } } } }
函数的返回值。——return 值或变量;要保持return后面的类型与函数的返回类型要一致。
递归。自己调自己。
语法思想:
返回类型 函数名(参数)
{
1.结束递归的判断。
2.递归运算:函数名(参数);
}
static void Main(string[] args) { Test(0); } static void Test(int a) { if(a >5) { return; } a++; Console.WriteLine("正在做第"+a+"个梦"); Test(a); Console.WriteLine("第"+a+"个梦醒了"); } } }
七、结构体:
为什么要用结构体?自己定义的复合类型,更好地模拟生活中的各种对象。
定义
struct 结构体名
{
public 类型 子变量名;
public 类型 子变量名;
....
}
使用:
结构体名 结构体变量 = new 结构体名();
结构体变量.子变量 = 值;
结构体变量.子变量;
结构体数组:
结构体类型[] 数组名 = new 结构体类型[长度];
数组名[下标].子变量
如何使用循环来操作结构体数组。
例子:学生成绩统计
namespace ConsoleApplication1 { struct XueSheng { public int XueHao; public string XingMing; public double YuWen; public double ShuXue; public double WaiYu; public double ZongFen; public int MinCi; } class Class1 { static void Main(string[] args) { //int[] a = new int[5]; XueSheng[] s = new XueSheng[5]; //输入 ShuRu(s); //排序 PaiXu(s); //填名次 XieMingCi(s); //输出显示 ShuChu(s); } private static void ShuChu(XueSheng[] s) { for (int i = 0; i < s.Length; i++) { Console.WriteLine(s[i].XueHao + "\t" + s[i].XingMing + "\t" + s[i].YuWen + "\t" + s[i].ShuXue + "\t" + s[i].WaiYu + "\t" + s[i].ZongFen + "\t" + s[i].MinCi); } } private static void XieMingCi(XueSheng[] s) { for (int i = 0; i < s.Length; i++) { s[i].MinCi = i + 1; } } private static void PaiXu(XueSheng[] s) { for (int i = 1; i <= s.Length - 1; i++) { for (int j = 1; j <= s.Length - i; j++) { if (s[j].ZongFen > s[j - 1].ZongFen) { XueSheng temp = s[j]; s[j] = s[j - 1]; s[j - 1] = temp; } } } } private static void ShuRu(XueSheng[] s) { for (int i = 0; i < s.Length; i++) { Console.WriteLine("正在输入第" + (i + 1) + "个学生的信息"); s[i].XueHao = i + 1; Console.Write("姓名:"); s[i].XingMing = Console.ReadLine(); Console.Write("语文:"); s[i].YuWen = Convert.ToDouble(Console.ReadLine()); Console.Write("数学:"); s[i].ShuXue = Convert.ToDouble(Console.ReadLine()); Console.Write("外语:"); s[i].WaiYu = Convert.ToDouble(Console.ReadLine()); //总分: s[i].ZongFen = s[i].YuWen + s[i].ShuXue + s[i].WaiYu; } } } }
对战的小游戏
struct Roal { public int blood; public int def; public int mov1; public int mov2; } class 黄金圣斗士:米罗VS沙加 { static void Main(string[] args) { Roal cast1, cast2; //角色1的属性 cast1.blood = 1000; cast1.def = 50; cast1.mov1 = 120; cast1.mov2 = 160; //角色2的属性 cast2.blood = 1000; cast2.def = 60; cast2.mov1 = 110; cast2.mov2 = 130; //颜色设置 Console.ForegroundColor = ConsoleColor.Blue; Console.BackgroundColor = ConsoleColor.White; Console.Clear(); //输入操作 //角色1攻击 while(cast1.blood >0&&cast2.blood >0) { Console.WriteLine("请米罗发招:a-天蝎意念波 s-猩红毒针"); string m = Console.ReadLine(); Random n1 = new Random(); if (m == "a") { int s = n1.Next(60);//角色2的防御值 int s2 = cast1.mov1 - s;//攻击效果 cast2.blood -= s2;//角色2血量减少 } else if (m == "s") { int s = n1.Next(60);//角色2的防御值 int s2 = cast1.mov2 - s;//攻击效果 cast2.blood -= s2;//角色2血量减少 } //角色2攻击 Console.WriteLine("请沙加发招:k-天舞宝轮 l-六道轮回"); string h= Console.ReadLine(); Random i = new Random(); if (h == "k") { int s = i.Next(50);//角色1的防御值 int s2 = cast1.mov1 - s;//攻击效果 cast1.blood -= s2;//角色1血量减少 } else if (h == "l") { int s = n1.Next(50);//角色1的防御值 int s2 = cast1.mov2 - s;//攻击效果 cast1.blood -= s2;//角色1血量减少 } else { Console.WriteLine("发招失败"); } Console.Clear(); cast1.blood = cast1.blood < 0 ? 0 : cast1.blood ;//控制血量不要小于零 cast2.blood = cast2.blood < 0 ? 0 : cast2.blood;//控制血量不要小于零 Console.WriteLine ("米罗血量:{0}",cast1.blood ); Console.WriteLine("沙加血量:{0}",cast2.blood); } //输出对殴结果 if (cast1.blood == 0 && cast2.blood == 0) { Console.WriteLine("同归于尽"); } else if (cast1.blood ==0) { Console.WriteLine("沙加打败米罗"); } else { Console.WriteLine("米罗打败沙加,成为最强黄金圣斗士"); } }