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

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

المطلوب

أكتب برنامج يطلب من المستخدم إدخال ثلاث أرقام و خزنها في ثلاث متغيرات (a - b - c ), ثم يعرض له أصغر رقم تم إدخاله.
مثال: إذا قام المستخدم بإدخال الأرقام 2, 7 و 5 فستكون النتيجة كالتالي.

Enter a: 2
Enter b: 7
Enter c: 5
The min number is: 2
	

الحل بلغة C#

الطريقة الأولى لحل التمرين.

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, minimum;
Console.Write("Enter a: ");
a = Int32.Parse(Console.ReadLine());
Console.Write("Enter b: ");
b = Int32.Parse(Console.ReadLine());
Console.Write("Enter c: ");
c = Int32.Parse(Console.ReadLine());
minimum = (a < b) ? a : b;
minimum = (minimum < c) ? minimum : c;
Console.WriteLine("The min number is: " + minimum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, minimum; Console.Write("Enter a: "); a = Int32.Parse(Console.ReadLine()); Console.Write("Enter b: "); b = Int32.Parse(Console.ReadLine()); Console.Write("Enter c: "); c = Int32.Parse(Console.ReadLine()); minimum = (a < b) ? a : b; minimum = (minimum < c) ? minimum : c; Console.WriteLine("The min number is: " + minimum); Console.ReadKey(); } }

الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, minimum;
Console.Write("Enter a: ");
a = Int32.Parse(Console.ReadLine());
Console.Write("Enter b: ");
b = Int32.Parse(Console.ReadLine());
Console.Write("Enter c: ");
c = Int32.Parse(Console.ReadLine());
if (a < b && a < c)
{
minimum = a;
}
else if (b < a && b < c)
{
minimum = b;
}
else
{
minimum = c;
}
Console.WriteLine("The min number is: " + minimum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, minimum; Console.Write("Enter a: "); a = Int32.Parse(Console.ReadLine()); Console.Write("Enter b: "); b = Int32.Parse(Console.ReadLine()); Console.Write("Enter c: "); c = Int32.Parse(Console.ReadLine()); if (a < b && a < c) { minimum = a; } else if (b < a && b < c) { minimum = b; } else { minimum = c; } Console.WriteLine("The min number is: " + minimum); Console.ReadKey(); } }

الطريقة الثالث لحل التمرين و الحصول على نفس النتيجة.

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, minimum;
Console.Write("Enter a: ");
a = Int32.Parse(Console.ReadLine());
Console.Write("Enter b: ");
b = Int32.Parse(Console.ReadLine());
Console.Write("Enter c: ");
c = Int32.Parse(Console.ReadLine());
if (a < b && a < c)
{
minimum = a;
}
else
{
minimum = b;
}
if (minimum > c)
{
minimum = c;
}
Console.WriteLine("The min number is: " + minimum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, minimum; Console.Write("Enter a: "); a = Int32.Parse(Console.ReadLine()); Console.Write("Enter b: "); b = Int32.Parse(Console.ReadLine()); Console.Write("Enter c: "); c = Int32.Parse(Console.ReadLine()); if (a < b && a < c) { minimum = a; } else { minimum = b; } if (minimum > c) { minimum = c; } Console.WriteLine("The min number is: " + minimum); Console.ReadKey(); } }

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الأرقام 2, 7 و 5 عند التشغيل.

Enter a: 2
Enter b: 7
Enter c: 5
The min number is: 2