I'm a student and new to c#, I have tried to search an element and print index of the element, but I don't know why it's doesn't work.
this is my code
string temp = " ";
int hold;
const int SIZE = 5;
int[] a = new int[SIZE];
bool found = false;
int num;
//reading num from user
for (int i = 0; i < SIZE; i++)
{
a[i] = int.Parse(Interaction.InputBox("Enter the array values: "));
}
// print values
for (int i = 0; i < SIZE; i++)
{
temp = temp + a[i] + " ";
}
temp = temp + "\n";
MessageBox.Show("array values: \n" + temp);
// this part I want it
num = int.Parse(Interaction.InputBox("Enter the value to found "));
for (int j = 0; j < SIZE; j++)
{
if (a[j] == num)
{
MessageBox.Show("Searched value found in array");
}
}
MessageBox.Show("Searched value not found in array");
and thank you
Consider:
bool isFound = false;
for (int j = 0; j < a.Length; j++)
{
if (a[j] == num)
{
isFound = true;
}
}
if(isFound)
{
}
else
{
}
Just finish it off..
Then consider how to stop the loop when you found what you want (efficiency: don't keep searching for something you already found)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.