一、练习题
1.取字串数组中最长的元素。
private static void Main(string[] args)
{
string[] str = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
Console.WriteLine(GetSeri(str));
Console.ReadKey();
}
public static string GetSeri(string[] str)
{
int j = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i].Length > j)
{
j = i;
}
}
return str[j];
}
2.取整数数组中的平均值,保留两位小数
private static void Main(string[] args)
{
int[] intA = { 2, 3, 4, 5, 6, 7 };
double avg = GetAvg(intA);
Console.WriteLine("{0:0.00}", avg);//4.50
Console.WriteLine(avg.ToString("0.00"));//4.50
Console.WriteLine(Convert.ToDouble(avg.ToString("0.00")));//4.5
Console.ReadKey();
}
public static double GetAvg(int[] intB)
{
double sum = 0;//提升为doulbe
for (int i = 0; i < intB.Length; i++)
{
sum += intB[i];
}
return 1.00 * sum / intB.Length;
}
3.方法A判断输入是否为质数,方法B要求只能输入数字,否则一直输入
private static void Main(string[] args)
{
int num = GetNum();
if (GetPrime(num)) { Console.WriteLine("是质数"); }
else { Console.WriteLine("不是质数"); }
Console.ReadKey();
}
public static int GetNum()
{
int a;
while (true)
{
Console.WriteLine("请输入一个正整数");
try
{
a = Convert.ToInt32(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("不是数字,请重新输入!");
}
}
return a;
}
public static bool GetPrime(int a)
{
if (a <= 2) { return false; }
else
{
for (int i = 2; i <= a / 2; i++)
{
if (a % i == 0) { return false; }
}
return true;
}
}
4.输入分数,判断优良等级别
private static void Main(string[] args)
{
while (true)
{
try
{
Console.WriteLine("请输入分数(大于0):");
int num = Convert.ToInt32(Console.ReadLine());
if (num >= 0)
{
Console.WriteLine(GetLevel(num));
break;
}
}
catch
{
Console.WriteLine("不是数字请重输入!");
}
}
Console.ReadKey();
}
public static string GetLevel(int score)
{
switch (score / 10)
{
case 10:
case 9: return "优";
case 8: return "良";
case 7: return "中";
case 6: return "差";
default: return "不及格";
}
}
5.数组反转。注意参数不加ref也可传回到实参
private static void Main(string[] args)
{
string[] str = { "中国", "美国", "巴西", "澳大利亚", "加拿大" };
//str = str.Reverse().ToArray();
//Array.Reverse(str);
GetReverse(str);
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
Console.ReadKey();
}
public static void GetReverse(string[] str)
{//参数不加ref,对数组一样可以影响实参
for (int i = 0; i < str.Length / 2; i++)
{
string temp = str[i];
str[i] = str[str.Length - 1 - i];
str[str.Length - 1 - i] = temp;
}
}
6.返回圆的周长与面积。
private static void Main(string[] args)
{
double r = 5, peri, area;
area = GetParams(r, out peri);//若未定义也可out double peri
Console.WriteLine("周长为{0},面积为{1}.", peri, area);
Console.ReadKey();
}
public static double GetParams(double r, out double p)
{//out参数可前可后。
p = 2 * 3.14 * r;
return 3.14 * r * r;
}
二、飞行棋
1.规则
蛇形地图,两人轮流掷骰,谁达终点即胜。
图例:幸运轮盘:◎ 地雷:☆ 暂停:△ 时空隧道:◆
游戏规则:
如果玩家A踩到玩家B,玩家B退6格
如果玩家踩中地雷。退6格
如果玩家踩进时空隧道,进10格
如果玩家踩到幸运轮盘, 1 交换位置,2 轰炸对方(使对方退6格)
如果玩家踩到了暂停,暂停一个回合
如果玩家踩到了方块,走到当前位置,什么都不发生
2、程序
internal class Program
{
// 普通□ 幸运轮盘:◎ 地雷:★ 暂停:▲ 时空隧道:卐
public static int[] Maps = new int[100]; //地图
public static int[] playerPos = new int[2]; //玩家坐标
public static string[] playerNames = new string[2];//玩家name
public static bool[] b = new bool[2];//暂停,0,1
private static void Main(string[] args)
{
GameStart();
InitMap();
DrawMap();
InputPlayer();//输入玩家名字
Console.Clear();
InitMap();
DrawMap();
while (playerPos[0] < 99 && playerPos[1] < 99)
{
if (b[0] == false) PlayGame(0);
else b[0] = false;
if (b[1] == false) PlayGame(1);
else b[1] = false;
}
Win();
Console.ReadKey();
}
public static void Win()
{
if (playerPos[0] >= 99)
{
Console.WriteLine("玩家{0}取得了胜利!!", playerNames[0]);
}
else
{
Console.WriteLine("玩家{0}取得了胜利!!", playerNames[1]);
}
}
public static void PlayGame(int n)
{
Random r = new Random();
int step = r.Next(1, 7);//[1,7)
Console.WriteLine("玩家{0}按任意键开始掷骰子:", playerNames[n]);
Console.ReadKey(true);//不显示按键激活
Console.WriteLine(" 掷出了{0},准备前行{0}步", step);
playerPos[n] += step;
Console.ReadKey(true);
if (playerPos[n] == playerPos[1 - n])//踩中另一玩家退6格
{
Console.WriteLine(" 玩家{0}被踩中退6格", playerNames[1 - n]);
playerPos[1 - n] -= 6;
}
else//是否关卡
{
ChangePos();
switch (Maps[playerPos[n]])
{
case 0:
Console.WriteLine(" 玩家{0}踩到了方块,安全.", playerNames[n]);
break;
case 1:
Console.WriteLine(" 玩家{0}踩到了轮盘,选择1--交换位置,2--轰炸对方退6格", playerNames[n]);
string sel = Console.ReadLine();
while (sel != "1" && sel != "2")
{
if (sel == "1")
{
Console.WriteLine(" 你选择了交换位置");
int temp = playerPos[n];
playerPos[n] = playerPos[1 - n];
playerPos[1 - n] = temp;
Console.WriteLine(" 位置交换完成.");
}
else if (sel == "2")
{
playerPos[1 - n] -= 6;
Console.WriteLine(" 玩家{0}退6格完成.", playerNames[1 - n]);
}
else
{
Console.WriteLine(" 无效输入,请重新输入1--交换位置,2--轰炸对方退6格");
sel = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine(" 玩家{0}踩到了地雷退6格", playerNames[n]);
playerPos[n] -= 6;
break;
case 3:
Console.WriteLine(" 玩家{0}踩到了暂停,暂停一回合。", playerNames[n]);
b[n] = true;
break;
case 4:
Console.WriteLine(" 玩家{0}踩到了时空隧道,进10格", playerNames[n]);
playerPos[n] += 10;
break;
}//switch
ChangePos();
}//if
Console.ReadKey(true);
Console.Clear();
DrawMap();
}
public static void ChangePos()
{
if (playerPos[0] < 0)
{
playerPos[0] = 0;
}
else if (playerPos[0] > 99)
{
playerPos[0] = 99;
}
if (playerPos[1] < 0)
{
playerPos[1] = 0;
}
else if (playerPos[1] > 99)
{
playerPos[1] = 99;
}
}
public static void InputPlayer()
{
Console.WriteLine();
do
{
Console.WriteLine("请输入玩家A的姓名(不能为空):");
playerNames[0] = Console.ReadLine();
} while (playerNames[0] == "");
do
{
Console.WriteLine("请输入玩家B的姓名(不能为空或与玩家A相同):");
playerNames[1] = Console.ReadLine();
} while (playerNames[1] == "" || playerNames[0] == playerNames[1]);
Console.WriteLine();
Console.WriteLine("玩家A代表:{0}", playerNames[0]);
Console.WriteLine("玩家B代表:{0}", playerNames[1]);
}//输入玩家姓名
public static void DrawMap()
{
Console.WriteLine("图例:幸运轮盘:◎ 地雷:★ 暂停:▲ 时空隧道:卐 ");
#region 第一横行
for (int i = 0; i <= 29; i++)
{
Console.Write(DrawStringMap(i));
}//for
Console.WriteLine();
#endregion 第一横行
#region 第一纵竖
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
Console.WriteLine(DrawStringMap(i));
}
#endregion 第一纵竖
#region 第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion 第二横行
#region 第二纵竖
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion 第二纵竖
#region 第三横行
for (int i = 70; i < 100; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion 第三横行
}//刷新地图
public static string DrawStringMap(int i)
{
if (playerPos[0] == playerPos[1] && playerPos[1] == i)//位置重合
{
return "<>";
}
else if (playerPos[0] == i)
{
return "A";
}
else if (playerPos[1] == i)
{
return "B";
}
else
{
switch (Maps[i])
{
case 0:
return "□";
case 1:
return "◎";
case 2:
return "★";
case 3:
return "▲";
default:
return "卐";
}//switch
}//if
}//位置字符
public static void InitMap()
{
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };
for (int i = 0; i < luckyturn.Length; i++)//幸运轮盘:◎
{
Maps[luckyturn[i]] = 1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷:★
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 };//暂停:▲
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道:卐
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
}//初始化地图
public static void GameStart()
{
Console.Clear();
Console.WriteLine("*************************");
Console.WriteLine("*************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**********飞行棋*********");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("*************************");
Console.WriteLine("*************************");
}//游戏头
}