ویرگول
ورودثبت نام
علیرضا فرقدانی
علیرضا فرقدانی
خواندن ۸ دقیقه·۳ سال پیش

ساخت یک مستطیل با c#

سلام به همگی امرو اومدیدم یک کد رو به شما اموزش بدیم که باهاش می تونید یک مستطیل داخل کنسول C# درست کنید

کد به این صورت کار می کنه که عرض و طول مستطیل رو میگیره و بعد با متود for میاد و شکل رو با اشکال مختلف تشکیل می ده


عکس از محیط بزنامه
عکس از محیط بزنامه


کد چطور کار می کنه

داخل کد یک متد حلقه است باعث می شه برنامه هی تکرار می شه و چیز خاصی نیست بعدش یک سری input ها هستن که عدد ها رو از مخاطب می گیرن متغیر های این برنامه عبارت اند از

int w عددی که بعد از هر عملیات یکی بهش اضافه می شه تا شکل ایجاد بشه

int h عددی که بعد از هر عملیات یکی بهش اضافه می شه تا شکل ایجاد بشه

int height ارتفاع ما

int width عرض ما


کد ها برنامه



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace makeshape { class Program { static void Main(string[] args) { while (true) { //----------------------------- Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Black; string shape = &quot&quot Console.Write(&quotheight : &quot); int width = Convert.ToInt32(Console.ReadLine()); Console.Write(&quotwidth : &quot); int height = Convert.ToInt32(Console.ReadLine()); Console.Clear(); Console.ResetColor(); if (width > 0 || height > 0) { for (int w = 2; w < width; w++) { if (w == 2) { for (int i = 0; i < height; i++) { shape += &quot@ &quot } shape += &quot\n&quot } for (int h = 2; h < height; h++) { if (h == 2) { shape += &quot@ &quot } shape += &quot# &quot if (h == height - 1) { shape += &quot@ &quot } } shape += &quot\n&quot if (w == width - 1) { for (int i = 0; i < height; i++) { shape += &quot@ &quot } shape += &quot\n&quot } } Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(shape); Console.WriteLine(&quotshape Area : &quot+width * height); Console.ReadLine(); Console.Clear(); Console.ResetColor(); } else { Console.Clear(); Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(&quot|___> size can't = 0 !!&quot); Thread.Sleep(1500); Console.Clear(); Console.ResetColor(); } //--------------------- } } } }


ساخت شکل با cconsoleکنسول
سلام من علیرضام یک برنامه نویس
شاید از این پست‌ها خوشتان بیاید