日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

循环嵌套练习题

發(fā)布時(shí)間:2023/12/18 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 循环嵌套练习题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//BOSS://讓用戶輸入一個(gè)奇數(shù),打印菱形,最長(zhǎng)的行內(nèi)容個(gè)數(shù)為用戶輸入的個(gè)數(shù),并且由英文字母拼接而成//比如用戶輸入了7// A// ABA// ABCBA// ABCDCBA// ABCBA// ABA// A//1、接收并判斷用戶輸入的是不是數(shù)字 try{#region 解法一//Console.Write("請(qǐng)輸入一個(gè)奇數(shù):");//int a = Convert.ToInt32(Console.ReadLine());//if (a % 2 != 0)//{// for (int i = 1; i <= (a + 1) / 2; i++)// {// for (int b = 1; b <= ((a + 1) / 2 - i); b++)// {// Console.Write(" ");// }// char c = 'A';// for (int d = 1; d < i; d++)// {// Console.Write(c);// c++;// }// for (int e = 1; e <= 26; e++)// {// Console.Write(c);// if (c == 'A')// {// break;// }// c--;// }// Console.WriteLine();// }// for (int i = 1; i < (a + 1) / 2; i++)// {// for (int b = 1; b <= i; b++)// {// Console.Write(" ");// }// char c = 'A';// for (int d = 1; d < (a + 1) / 2 - i; d++)// {// Console.Write(c);// c++;// }// for (int e = 1; e <= 26; e++)// {// Console.Write(c);// if (c == 'A')// {// break;// }// c--;// }// Console.WriteLine();// }//}#endregion#region 解法二 上半部分 Console.Write("請(qǐng)輸入一個(gè)奇數(shù):");int a = Convert.ToInt32(Console.ReadLine());if (a % 2 != 0)//是奇數(shù),執(zhí)行這個(gè)if里面的代碼 {for (int i = 1; i <= (a + 1) / 2; i++)//上半部分行數(shù),(a+1)/2 代表 上半部分需要打印的行數(shù)。 {char ch = 'A';string end = "";int b = ((i * 2 - 1) + 1) / 2 - 1;//開始 -- 的數(shù)值bool isok = false;int count = 0;for (int j = 1; j <= ((a + 1) / 2) - i; j++)//拼接每行打印的空格數(shù) {end += " ";}for (int j = 1; j <= i * 2 - 1; j++)//拼接每行打印的字母數(shù) {end += ch;if (count == b)//判斷是不是該 -- 了 {isok = true;}if (isok)//滿足條件,執(zhí)行這個(gè) if 里面的代碼 {if (ch == 'A'){ch = 'Z';}else//條件不成立 {ch--;}}else{if (ch == 'Z'){ch = 'A';}else{ch++;}count++;}}Console.WriteLine(end);}}#endregion#region 解法二 下半部分for (int i = 1; i < (a + 1) / 2; i++){char ch = 'A';string end1 = "";int b = ((a - i * 2) + 1) / 2 - 1;int count = 0;bool isok = false;for (int j = 1; j <= i; j++){end1 += " ";}for (int j = 1; j < (a - i * 2) + 1; j++){end1 += ch;if (count == b){isok = true;}if (isok){if (ch == 'A'){ch = 'Z';}else{ch--;}}else{if (ch == 'Z'){ch = 'A';}else{ch++;}count++;}}Console.WriteLine(end1);}#endregion}catch{Console.WriteLine("輸入有誤!");}Console.ReadLine();

?

轉(zhuǎn)載于:https://www.cnblogs.com/123lucy/p/5523144.html

總結(jié)

以上是生活随笔為你收集整理的循环嵌套练习题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。