دریافت ورودی در #C (قسمت دوم)

جلسه ششم (قسمت دوم) : دریافت ورودی در #C

آموزش برنامه نویسی C Shorp

قبل از مطالعه ای جلسه، بر روی این لینک کلیک کنید و جلسه قبل را مطالعه کنید.

در جلسه قبل، یاد گرفتیم که چگونه از کاربر ورودی بگیریم و به چه صورت آن را چاپ کنیم. در جلسه قبل، نوع (data Type یا دیتا تایپ هایی که از کاربر دریافت کردیم، String یا رشته (متن) بود. این بار میخواهیم برنامه ای بنویسیم که از کاربر یک مقدار عددی دریافت کند.

مثال: برنامه ای بنویسید که از کاربر بخواهد سن خود را وارد کند.

۱- ابتدا میخواهیم زمانی که برنامه اجرا شد، متن “Enter Your Age” برای کاربر ظاهر شود. این متن از کاربر میخواهد سن خود را وارد کند.

static void Main(string[] args)
{
Console.WriteLine(“Enter your Age”);
}

۲- پس از اینکه رشته یا متن “Enter Your Age” برای کاربر ظاهر شد، کاربر میفهمد که باید سن خود را وارد کند. اما قبل از آن باید یک متغیر ایجاد کنیم تا مقداری که کاربر به عنوان ورودی وارد میکند، درون آن قرار بگیرد. نوع data Type ورودی، عدد میباشد. پس متغیر ما از نوع int است.  نام متغیر را age میگذاریم.

static void Main(string[] args)
{
Console.WriteLine(“Enter your Age”);
int age;
}

۳- گفتیم برای اینکه برنامه این قابلیت را داشته باشد تا بتواند از کاربر ورودی بگیرد، باید از دستور ()Console.ReadLine استفاده کنیم. اکنون متغیر int را مساوی با این دستور قرار میدهیم. به این صورت:  ;()int age = Console.ReadLine، این عبارت، یعنی مقداری را که به عنوان ورودی از کاربر دریافت شد، داخل متغیر age قرار بگیرد.

static void Main(string[] args)
{
Console.WriteLine(“Enter your Age”);
int age = Console.ReadLine();
}

اما در این قسمت ما با یک خظا مواجه میشویم. برنامه به ما هشدار میدهد که مقدار ورودی که قرار است از کاربر دریافت کنیم، باید از نوع String به نوع int تبدیل بشود. حالا این به چه معناست؟  ممکن است برایتان یک سوال پیش بیاید، متغیر ما از نوع int میباشد، مقدار ورودی یا همان عددی که کاربر به عنوان سن خود در برنامه وارد میکند از نوع int است. پس چرا برنامه ورودی را از نوع int نمیشناسد؟

به این دلیل که کاربر زمانی که سن خود را در محیط Console، به صورت یک عدد وارد میکند، برنامه این ورودی را به عدد نمیشناسد. برنامه این ورودی را به عنوان یک String یا رشته (متن) میشناسد. این پیغام خطا، از ما میخواهد عددی را که کاربر به برنامه داده، به نوع int یا اعداد صحیح تبدیل شود.

درست است که سن کاربر یک عدد صحیح است اما برنامه در این مرحله، این عدد را از نوع String یا رشته (متن) میشناسد و از شما میخواهد این مشکل را اصلاح کنید تا بتواند اعداد را از نوع int بشناسد.

نکته: ممکن  است برایتان سوال پیش بیاید که ما هنوز هیچ ورودی به برنامه نداده ایم. پس چرا برنامه از ما میخواهد مقدار ورودی را از String به int تبدیل کنیم؟

جواب: شما بلافاصله بعد از اینکه عبارت ;()int age = Console.ReadLine را ایجاد میکنید، برنامه میفهمد که میخواهید یک مقدار عددی از کاربر بگیرید. پس از شما میخواهد، برنامه را طوری پیاده سازی کنیم که برنامه مقدار ورودی کاربر را از نوع int بشناسد

۴- برای تبدیل داده ها از نوع String (رشته) به نوع int (اعداد صحیح)، از دستور Convert.ToInt32 استفاده میکنیم. سپس دستور ()Console.WriteLine، درون پرانتز قرار میگیرند. همانند زیر:

static void Main(string[] args)
{
Console.WriteLine(“Enter your Age”);

int age = Convert.ToInt32 (Console.ReadLine());
}

نکته: از عبارت Convert.ToInt32 ، برای تبدیل یک مقدار، به یک عدد صحیح ۳۲ بیتی استفاده میشود.

۵- هم اکنون، میتوانید برنامه را اجرا کنید، ابتدا پیفام  “Enter your Age” برای شما نمایش داده میشود. سن خود را به صورت یک عدد وارد کنید. برای مثال ۲۰،

دریافت ورودی از کاربر در سی شارپ

پس از اجرای برنامه، در محیط Consoel، پس از اینکه برنامه پیغام Enter your age را به نمایش گذاشت، عدد ۲۰ را به عنوان سن خودم وارد کردم.

۶- سپس بر روی کلید Enter را کلیک کنید. هم اکنون شما یک ورودی از نوع عدد صحیح به برنامه دادید. این عدد، به عنوان یک عدد صحیح، درون متغیر age قرار داده شد.

نحوه دریافت ورودی از کاربر در C#

محیط کلی کدنویسی

در جلسه آینده، با مثال های بیشتری آشنا خواهیم شد. با ما همراه باشید.

 

ارسال دیدگاه

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *


Warning: file_put_contents(): Only -1 of 81 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1043 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 137870 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 242 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 116126 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 292 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 10982 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 25669 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 739 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 8901 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 2368 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 129 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 59046 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 350 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1058 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 57 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 2279 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 267177 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 160457 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 11343 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 4186 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 153101 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 5676 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 21488 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 53640 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1391 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 451276 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 508 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 5860 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1558916 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 60262 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 26702 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 6435 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1051 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 3679 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 58071 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 983 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 975 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 611 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 237 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 12607 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 254 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 77 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 3235 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 13578 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 9638 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 293 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 4037 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 1722 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 180 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 3815 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Warning: file_put_contents(): Only -1 of 15074 bytes written, possibly out of free disk space in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177

Fatal error: Uncaught ErrorException: file_put_contents(): write of 8192 bytes failed with errno=122 Disk quota exceeded in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php:177 Stack trace: #0 [internal function]: litespeed_exception_handler(8, 'file_put_conten...', '/home/farkia/do...', 177, Array) #1 /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php(177): file_put_contents('/home/farkia/do...', '<!DOCTYPE html>...', 8) #2 /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/optimizer.cls.php(135): LiteSpeed\File::save('/home/farkia/do...', '<!DOCTYPE html>...', true, true) #3 /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/optimize.cls.php(838): LiteSpeed\Optimizer->serve('https://farkian...', 'js', true, Array) #4 /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/optimize.cls.php(388): LiteSpeed\Optimize->_ in /home/farkia/domains/farkiantech.com/public_html/wp-content/plugins/litespeed-cache/src/file.cls.php on line 177