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: 1
Enter b: 2
Enter c: 5
The max number is: 5
	

الحل بلغة C#

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

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, maximum;
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());
maximum = (a > b) ? a : b;
maximum = (maximum > c) ? maximum : c;
Console.WriteLine("The max number is: " + maximum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, maximum; 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()); maximum = (a > b) ? a : b; maximum = (maximum > c) ? maximum : c; Console.WriteLine("The max number is: " + maximum); Console.ReadKey(); } }

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

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, maximum;
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)
{
maximum = a;
}
else if (b > a && b > c)
{
maximum = b;
}
else
{
maximum = c;
}
Console.WriteLine("The max number is: " + maximum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, maximum; 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) { maximum = a; } else if (b > a && b > c) { maximum = b; } else { maximum = c; } Console.WriteLine("The max number is: " + maximum); Console.ReadKey(); } }

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

using System;
class Program
{
static void Main(string[] args)
{
int a, b, c, maximum;
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)
{
maximum = a;
}
else
{
maximum = b;
}
if (maximum < c)
{
maximum = c;
}
Console.WriteLine("The max number is: " + maximum);
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int a, b, c, maximum; 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) { maximum = a; } else { maximum = b; } if (maximum < c) { maximum = c; } Console.WriteLine("The max number is: " + maximum); Console.ReadKey(); } }

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

Enter a: 1
Enter b: 2
Enter c: 5
The max number is: 5