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

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

المطلوب

أكتب برنامج يطلب من المستخدم إدخال أي رقم يريد للبحث عنه بداخل مصفوفة أرقام أحادية (ذات بعد واحد) جاهزة.
بعدها سيقوم البرنامج بطباعة ما إن كانت القيمة موجودة في المصفوفة أم لا.

ملاحظة: بمجرد أن يتم إيجاد القيمة المراد البحث عنها فإنه يجب إيقاف البحث.


الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 5, 3, 2, 4, 7, 2 };
int x;
bool isExist = false;
Console.Write("Enter a number: ");
x = Int32.Parse(Console.ReadLine());
for (int i = 0; i < array.Length; i++)
{
if (array[i] == x)
{
isExist = true;
break;
}
}
if (isExist)
{
Console.WriteLine("'" + x + "' is exists in the array");
}
else
{
Console.WriteLine("'" + x + "' is not exist in the array");
}
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int[] array = { 1, 2, 5, 3, 2, 4, 7, 2 }; int x; bool isExist = false; Console.Write("Enter a number: "); x = Int32.Parse(Console.ReadLine()); for (int i = 0; i < array.Length; i++) { if (array[i] == x) { isExist = true; break; } } if (isExist) { Console.WriteLine("'" + x + "' is exists in the array"); } else { Console.WriteLine("'" + x + "' is not exist in the array"); } Console.ReadKey(); } }

سنحصل على النتيجة التالية في حال كانت المصفوفة تحتوي على نفس القيم المجهزة فيها و تم إدخال نفس القيمة التي تم تعليمها باللون الأصفر عند التشغيل.

Enter a number: 5
'5' is exists in the array