(8)C#传智:几个练习题和飞行棋(第八天)

news/2024/4/25 5:02:13/文章来源:https://blog.csdn.net/dzweather/article/details/129250504

一、练习题

    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.50Console.WriteLine(avg.ToString("0.00"));//4.50Console.WriteLine(Convert.ToDouble(avg.ToString("0.00")));//4.5Console.ReadKey();}public static double GetAvg(int[] intB){double sum = 0;//提升为doulbefor (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 periConsole.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];//玩家namepublic static bool[] b = new bool[2];//暂停,0,1private 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;}//switchChangePos();}//ifConsole.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));}//forConsole.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("*************************");}//游戏头}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_74859.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2023前端二面经典手写面试题

实现一个call call做了什么: 将函数设为对象的属性执行&删除这个函数指定this到函数并传入给定参数执行函数如果不传入参数&#xff0c;默认指向为 window // 模拟 call bar.mycall(null); //实现一个call方法&#xff1a; Function.prototype.myCall function(context…

LLaMA-META发布单卡就能跑的大模型

2023年2月25日&#xff0c;Meta使用2048张A100 GPU&#xff0c;花费21天训练的Transformer大模型LLaMA开源了。 1.4T tokenstakes approximately 21 days 以下是觉得论文中重要的一些要点 1&#xff09;相对较小的模型也可以获得不错的性能 研究者发现在给定计算能力限制的情…

《高性能MySQL》读书笔记(下)

目录 Mysql查询性能的优化 慢查询基础 优化数据访问 是否向数据库请求了不需要的数据 查询了不需要的记录 多表联查中返回全部列 MySQL是否在扫描额外的记录 重写查询的方式 切分查询&#xff08;重点&#xff09; 分解连接查询&#xff08;重点&#xff09; MySQL如…

史上最全的大数据开发八股文【自己的吐血总结】

自我介绍 我本硕都是双非计算机专业&#xff0c;从研一下开始学习大数据开发的相关知识&#xff0c;从找实习到秋招&#xff0c;我投递过100公司&#xff0c;拿到过10的offer&#xff0c;包括滴滴、字节、蚂蚁、携程、蔚来、去哪儿等大厂&#xff08;岗位都是大数据开发&#…

算法练习(七)数据分类处理

一、数据分类处理 1、题目描述&#xff1a; 信息社会&#xff0c;有海量的数据需要分析处理&#xff0c;比如公安局分析身份证号码、 QQ 用户、手机号码、银行帐号等信息及活动记录。采集输入大数据和分类规则&#xff0c;通过大数据分类处理程序&#xff0c;将大数据分类输出…

喜讯!华秋电子荣获第六届“蓝点奖”十佳分销商奖

2 月 25 日&#xff0c;由深圳市电子商会主办的2023 中国电子信息产业创新发展交流大会暨第六届蓝点奖颁奖盛典在深圳隆重举行。 图&#xff1a;华秋商城渠道总监杨阳&#xff08;右三&#xff09; 深圳市电子商会连续六年举办“蓝点奖”评选活动&#xff0c;旨在表彰对电子信…

高端电器新十年,求解「竞速突围」

竞争激烈的高端电器品牌们&#xff0c;平时王不见王&#xff0c;但也有例外。海尔、博西、海信、创维、方太、老板等等近乎中国电器行业所有一线品牌副总裁级别以上高层&#xff0c;2月22日都现身于上海&#xff0c;来参加一场由红星美凯龙攒起来的高端电器局&#xff0c;2023中…

能在软路由docker给部署搭建teamsperk服务器么?并且设置好ddns

参考链接(4条消息) 【个人学习总结】使用docker搭建Teamspeak服务器_blcurtain的博客-CSDN博客_teamspeak3 docker(⊙﹏⊙)哎呀&#xff0c;崩溃啦&#xff01; (tdeh.top)TeamSpeak服务器搭建与使用 - 缘梦の镇 (cmsboy.cn)Openwrt X86 docker运行甜糖-软路由,x86系统,openwrt…

虚拟数字人直播带货相比人工有哪些优势?

新经济时代的到来&#xff0c;彻底改变了传统的消费方式。虚拟数字人的出现&#xff0c;标志着新一波的消费升级到来。虚拟数字人直播带货&#xff0c;不仅降低了商家的带货成本&#xff0c;拉近了商家与消费者的距离&#xff0c;也给消费者带来全新的消费方式。 花西子虚拟形象…

如何查看Spring Boot各版本的变化

目录 1.版本 2.基础特性和使用 3.新增特性和Bug修复 1.版本 打开Spring官网&#xff0c;点进Spring Boot项目我们会发现在不同版本后面会跟着不同的标签&#xff1a; 这些标签对应不同的版本&#xff0c;其意思如下&#xff1a; GA正式版本&#xff0c;通常意味着该版本已…

k8s学习之路 | Day16 k8s 中的容器初探

文章目录容器镜像镜像名称镜像拉取策略私有仓库的拉取策略容器的环境变量和启动命令容器的环境变量容器的启动命令容器的生命周期钩子postStartpreStop容器的探针startupProbelivenessProbereadinessProbek8s 集群中最小的管理单元就是一个Pod&#xff0c;而Pod里面才是容器&am…

利用GPT-3 Fine-tunes训练专属语言模型

利用GPT-3 Fine-tunes训练专属语言模型 文章目录什么是模型微调&#xff08;fine-tuning&#xff09;&#xff1f;为什么需要模型微调&#xff1f;微调 vs 重新训练微调 vs 提示设计训练专属模型数据准备清洗数据构建模型微调模型评估模型部署模型总结什么是模型微调&#xff0…

JavaScript split()方法

JavaScript split()方法 目录JavaScript split()方法一、定义和用法二、语法三、参数值四、返回值五、更多实例5.1 省略分割参数5.2 使用limit参数5.3 使用一个字符作为分割符一、定义和用法 split() 方法用于把一个字符串分割成字符串数组。 二、语法 string.split(separat…

NCRE计算机等级考试Python真题(四)

第四套试题1、以下选项中&#xff0c;不属于需求分析阶段的任务是&#xff1a;A.需求规格说明书评审B.确定软件系统的性能需求C.确定软件系统的功能需求D.制定软件集成测试计划正确答案&#xff1a; D2、关于数据流图&#xff08;DFD&#xff09;的描述&#xff0c;以下选项中正…

RTMP的工作原理及优缺点

一.什么是RTMP&#xff1f;RTMP&#xff08;Real-Time Messaging Protocol&#xff0c;实时消息传输协议&#xff09;是一种用于低延迟、实时音视频和数据传输的双向互联网通信协议&#xff0c;由Macromedia&#xff08;后被Adobe收购&#xff09;开发。RTMP的工作原理是&#…

IP-GUARD控制台账户输入多次错误密码锁定后该如何解锁?

其他管理员账户给锁定了,暂时只能等其锁定时间到了才可以再次输入,默认是设置是锁定30min; 1、如果急需此账户查看,可以使用admin系统管理员账户登录控制台,在工具-账户中清除这个账户的密码,重新登录设置密码。

NIO与零拷贝

目录 一、零拷贝的基本介绍 二、传统IO数据读写的劣势 三、mmap优化 四、sendFile优化 五、 mmap 和 sendFile 的区别 六、零拷贝实战 6.1 传统IO 6.2 NIO中的零拷贝 6.3 运行结果 一、零拷贝的基本介绍 零拷贝是网络编程的关键&#xff0c;很多性能优化都离不开。 在…

【云原生kubernetes】k8s 常用调度策略使用详解

一、前言 通过之前的学习&#xff0c;我们了解到k8s集群中最小工作单位是pod&#xff0c;对于k8s集群来说&#xff0c;一个pod的完整生命周期是由一系列调度策略来控制&#xff0c;这些调度策略具体是怎么工作的呢&#xff1f;本文将详细讨论下这个问题。 二、k8s调度策略简介…

【多目标优化算法】多目标蚱蜢优化算法(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5;&#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密…

APP测试的7大注意点。

1. 运行 1&#xff09; App安装完成后的试运行&#xff0c;可正常打开软件。 2&#xff09; App打开测试&#xff0c;是否有加载状态进度提示。 3&#xff09; App⻚面间的切换是否流畅&#xff0c;逻辑是否正确。 4&#xff09; 注册 同表单编辑⻚面 用户名密码⻓度 …