2015-04-17 145 views
0

如果我輸入號碼5,它是在指數4它會給我not found第3次,然後索引將是一樣的,因爲我輸入的號碼。如何檢查數組是否包含在數組中並獲取其索引?

int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; 
int myNumber; 
int length = A.Length; 
Console.WriteLine("enter your number"); 
myNumber = Convert.ToInt32(Console.ReadLine()); 

for (int i = 0; i < length; i++) 
{ 
    if (myNumber == A[i]) 
    { 
     Console.WriteLine("the numer" + myNumber + "is present in the array at the index" +" "+ A[i]); 
    } 
    else 
    { 
     Console.WriteLine("the number you entered are not found"); 
    } 
    Console.ReadKey(); 
} 
+5

索引是''我''不是''a [i]'' –

回答

1

正確的程序:

int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; 
int myNumber; 
int length = A.Length; 
Console.WriteLine("enter your number"); 
myNumber = Convert.ToInt32(Console.ReadLine()); 

// ADDED 
bool found = false; 

for (int i = 0; i < length; i++) 
{ 
    if (myNumber == A[i]) 
    { 
     found = true; // ADDED 
     // On the far right of next row: Fixed A[i] -> i 
     Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i); 
     break; 
    } 
} 

// ADDED 
if (!found) 
{ 
    Console.WriteLine("the number you entered are not found"); 
} 

Console.ReadKey(); 

我希望/想你可以看到/理解沒有幫助的差異。

我補充一點,有解決問題的第二種方法:

int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; 
int myNumber; 
int length = A.Length; 
Console.WriteLine("enter your number"); 
myNumber = Convert.ToInt32(Console.ReadLine()); 

// MOVED OUTSIDE FOR 
int i = 0; 

for (; i < length; i++) 
{ 
    if (myNumber == A[i]) 
    { 
     // On the far right of next row: Fixed A[i] -> i 
     Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i); 
     break; 
    } 
} 

// ADDED 
if (i == length) 
{ 
    Console.WriteLine("the number you entered are not found"); 
} 

Console.ReadKey(); 

看到區別:found是沒有必要的,我們只使用i變量,有其「範圍」擴大。

0

你想改變你的代碼到這樣的東西。你的第一個錯誤是打印A [i]而不是i,你的第二個錯誤是在循環內打印未找到的消息。如果您將它的循環,將只打印一次外,同樣適用於Console.ReadKey()

int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; 
    int myNumber; 
    int length = A.Length; 
    Console.WriteLine("enter your number"); 
    myNumber = Convert.ToInt32(Console.ReadLine()); 

    bool isFound = false; 
    for (int i = 0; i < length; i++) 
    { 
     if (myNumber == A[i]) 
     { 

      Console.WriteLine("the numer" + myNumber + "is present in the array at the index" +" "+ i); 

      isFound = true; 
     }   
    } 

    if (!isFound) 
    { 
     Console.WriteLine("the number you entered are not found"); 
    } 

    Console.ReadKey(); 

} 

}

5

Array類有可以使用一個很好的IndexOf方法。它會返回您正在查找的值的索引位置,或者如果未找到該值,它將返回-1。

int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; 

Console.WriteLine("enter your number"); 
int myNumber = Convert.ToInt32(Console.ReadLine()); 

int indexLocation = Array.IndexOf(A, myNumber); 
if (indexLocation > -1) 
{ 
    Console.WriteLine("The number {0} was found at index location {1}", myNumber, indexLocation); 
} 
else 
{ 
    Console.WriteLine("The number {0} was not found", myNumber); 
} 
+0

它應該是'Array.IndexOf(A,myNumber);'。問題在於,雖然它在技術上是正確的(並且是解決問題的最佳方式),但它不是「形成性」的。這是一個「這裏採取解決所有問題的魔術功能」,而她的問題在哪裏理解如何退出循環。 – xanatos

+0

@xanatos:但這不是她問的。通過一切手段編輯問題文本和標題,使問題。 –

+0

@BinaryWorrier這將是我的錯誤,試圖制定問題標題,以配合她想要實現的目標。 – Alex

相關問題