Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثاني - حل التمرين الخامس بلغة C#

المطلوب

أكتب برنامج يطلب من المستخدم إدخال رقمين, ثم يعرض له قائمة خيارات يمكن تطبيقها على هذين الرقمين.
المستخدم سيكون عليه إدخال رقم الخيار فقط حتى يتم تنفيذه.

  • الخيار 0 لإيقاف البرنامج.
  • الخيار 1 لطباعة ناتج جمع العددين.
  • الخيار 2 لطباعة ناتج طرح العددين.
  • الخيار 3 لطباعة ناتج ضرب العددين.
  • الخيار 4 لطباعة ناتج قسمة العددين.

الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
double a, b;
int option = -1;
Console.Write("Enter first number: ");
a = Double.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
b = Double.Parse(Console.ReadLine());
Console.WriteLine(
"\nWrite down the operation number then press enter\n"
+ "0: To end the program.\n"
+ "1: To print the sum.\n"
+ "2: To print the subtraction.\n"
+ "3: To print the multiplication.\n"
+ "4: To print the division.");
while (option != 0)
{
Console.WriteLine("-------------------------");
Console.Write("Enter option: ");
option = Int32.Parse(Console.ReadLine());
switch (option)
{
case 0:
Console.WriteLine("Program end.");
break;
case 1:
Console.WriteLine(a + " + " + b + " = " + (a + b));
break;
case 2:
Console.WriteLine(a + " - " + b + " = " + (a - b));
break;
case 3:
Console.WriteLine(a + " * " + b + " = " + (a * b));
break;
case 4:
Console.WriteLine(a + " / " + b + " = " + (a / b));
break;
}
}
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { double a, b; int option = -1; Console.Write("Enter first number: "); a = Double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); b = Double.Parse(Console.ReadLine()); Console.WriteLine( "\nWrite down the operation number then press enter\n" + "0: To end the program.\n" + "1: To print the sum.\n" + "2: To print the subtraction.\n" + "3: To print the multiplication.\n" + "4: To print the division."); while (option != 0) { Console.WriteLine("-------------------------"); Console.Write("Enter option: "); option = Int32.Parse(Console.ReadLine()); switch (option) { case 0: Console.WriteLine("Program end."); break; case 1: Console.WriteLine(a + " + " + b + " = " + (a + b)); break; case 2: Console.WriteLine(a + " - " + b + " = " + (a - b)); break; case 3: Console.WriteLine(a + " * " + b + " = " + (a * b)); break; case 4: Console.WriteLine(a + " / " + b + " = " + (a / b)); break; } } Console.ReadKey(); } }

إذا قام المستخدم بإدخال الرقمين 1 و 4. ثم قام بتجربة كل الخيارات الموجودة في القائمة, فستكون النتيجة كالتالي.
قمنا بتعليم المعلومات التي أدخلها المستخدم باللون الأصفر و النتائج التي ظهرت له بعد إدخالها باللون الأزرق.

Enter first number:  1 
Enter second number:  4 

Write down the operation number then press enter
0: To end the program.
1: To print the sum.
2: To print the subtraction.
3: To print the multiplication.
4: To print the division.
-------------------------
Enter option:  1 
1 + 4 = 5
-------------------------
Enter option:  2 
1 - 4 = -3
-------------------------
Enter option:  3 
1 * 4 = 4
-------------------------
Enter option:  4 
1 / 4 = 0.25
-------------------------
Enter option:  0 
Program end.

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة