C#题型补充
2016-06-02 23:59
465 查看
让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成
Console.Write("请输入一个数字:"); try { int a = Convert.ToInt32(Console.ReadLine()); if (a % 2 != 0) { for (int i = 1; i <= (a+1)/2; i++)//菱形上半部分 { int c = ((i * 2 - 1) + 1) / 2 - 1;//中间值 string end = ""; char b='a'; int count = 0; bool d = false; for (int j = 1; j <=(a+1)/2-i; j++) { end += " "; } for (int j = 1; j <= i * 2 - 1; j++) { end += b; if (count == c) { d = true; } if (d) { if (b == 'A') { b = 'Z'; } else { b--; } } else { if (b == 'Z') { b = 'A'; } else { b++; } count++; } } Console.WriteLine(end); } for (int i = 1; i < (a + 1) / 2; i++)//菱形的下半部分 { char b = 'a'; int c = ((a - i * 2)+1) / 2 - 1; string end = ""; int count = 0; bool d = false; for (int j = 1; j <= i; j++) { end += " "; } for (int j = 1; j < (a - i * 2) + 1; j++) { end += b; if (count == c) { d = true; } if (d) { if (b == 'A') { b = 'Z'; } else { b--; } } else { if (b == 'Z') { b = 'A'; } else { b++; } count++; } } Console.WriteLine(end); } } else { Console.Write("输入错误"); } } catch { Console.Write("输入错误"); } Console.ReadLine();
用户输入一个字母,如D,那么就打印 ABCDCBA
//用户输入一个字母,如D,那么就打印 ABCDCBAConsole.Write("请输入一个字母");char a = Convert.ToChar(Console.ReadLine());char b = 'a';bool c = true;//判断b++或是b--//拼接要打印的内容for (int i = 1; i <= 26; i++){Console.Write(b);if (b == a){c= false;}if (c){b++;}else{b--;if (b == 'a' || b == 'A'){Console.Write(b);break;}}}Console.ReadLine();
相关文章推荐
- [置顶] 从0自学C#11--多线程创建方法汇总以及优缺点
- C#的方法
- C#使用SevenZipSharp库时如何去掉压缩文件中的目录文件夹
- Quartz.NET c# 教程 - 课程六:CronTrigger
- 设计模式之抽象工厂模式
- C#基础--局部类型Partial
- 30天C#基础巩固----查找XML文件元素
- C#项目中常用到的设计模式
- C#根据当前时间获取,本周,本月,本季度等时间段
- c#总复习一
- C#—实验10.3—工具栏和状态栏
- C#对Excel表的读取和写入
- C#线程总结
- C#中using的三种用法
- 【C#】Color颜色对照表
- C# this扩展方法
- C#.net使用GraphicsMagick
- C#使用GET、POST请求获取结果
- c#读取类库配置文件方法
- C#Thread的使用