المطلوب
أكتب برنامج يطلب من المستخدم إدخال رقمين, ثم يعرض له قائمة خيارات يمكن تطبيقها على هذين الرقمين.
المستخدم سيكون عليه إدخال رقم الخيار فقط حتى يتم تنفيذه.
الخيار
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(); } }
إذا قام المستخدم بإدخال الرقمين 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.
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.