2016-12-18 44 views
0

這更多的只是一個語法錯誤,因爲我的大部分代碼,因爲它是運行完全正常回報指數。問題是,如果在數組中找不到值,我希望程序將其打印出來。如果沒有程序在每行上寫出「未找到」,似乎無法找到編寫代碼部分的地方。以下是我的代碼和控制檯輸出。謝謝大家。搜索一個數組,如果一個特定的值時發現

代碼:

public static void main(String[ ] args) 
{ 
    final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 }; 
    final int[ ] EMPTY = new int[0]; 
    final int MINIMUM = 0; 
    final int MAXIMUM = 16; 

    int target; 

    System.out.println("Searching for numbers in an array."); 
    for (target = MINIMUM; target <= MAXIMUM; target++) 
    { 
     System.out.print("\nIs " + target + " in the array? "); 
     { 
      for (int index = 0; index < DATA.length; index++) 
      { 
       if (DATA[index] == target) 
        System.out.printf("Yes! %d was found at index [%d]", target, index); 
      } 
     } 
    } 

控制檯輸出:

Searching for numbers in an array... 

Is 0 in the array? 
Is 1 in the array? 
Is 2 in the array? Yes! 2 was found at index [0] 
Is 3 in the array? 
Is 4 in the array? Yes! 4 was found at index [1] 
Is 5 in the array? 
Is 6 in the array? Yes! 6 was found at index [2] 
Is 7 in the array? 
Is 8 in the array? Yes! 8 was found at index [3] 
Is 9 in the array? 
Is 10 in the array? Yes! 10 was found at index [4] 
Is 11 in the array? 
Is 12 in the array? Yes! 12 was found at index [5] 
Is 13 in the array? 
Is 14 in the array? Yes! 14 was found at index [6] 
Is 15 in the array? 
Is 16 in the array? 
+0

你想你的代碼,這樣做? –

+0

將數組轉換爲ArrayList並使用indexOf()方法不適合您? – n0rph3u5

回答

0

你應該使用一個變量裏面的for循環來告訴如果事情被發現了,你也應該退出的,一旦你發現了一些避免不必要的搜索。

public static void main(String[ ] args) 
{ 
    final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 }; 
    final int[ ] EMPTY = new int[0]; 
    final int MINIMUM = 0; 
    final int MAXIMUM = 16; 
    boolean foundElement; 
    int target; 

    System.out.println("Searching for numbers in an array."); 
    for (target = MINIMUM; target <= MAXIMUM; target++) 
    { 
     System.out.print("\nIs " + target + " in the array? "); 
     foundElement = false; 
     for (int index = 0; (index < DATA.length && !foundElement); index++) 
     { 
      foundElement = (DATA[index] == target); 
      if (foundElement) 
       System.out.printf("Yes! %d was found at index [%d]", target, index);      
     } 
     if (!foundElement) 
      System.out.printf(" Not found");  
    } 
} 

此外,您還可以閱讀,學習一些其他的方式來實現數組項搜索:4 ways to search object in array

0

速戰速決將是以下幾點:

  • 添加一個布爾變量

  • 設置布爾變量設置爲true,如果該元素被發現

  • 寫not found如果該元素未找到(即布爾變量是假的)

PS:不要忘了每個循環

0

使用此System.out.print("\nIs " + target + " in the array? ");前後進行布爾爲假:

boolean found = false; 
for (int index = 0; index < DATA.length; index++) { 
    if (DATA[index] == target) { 
     System.out.printf("Yes! %d was found at index [%d]", target, index); 
     found = true; 
     break; 
    } 
} 
if (!found) { 
    System.out.printf("No! %d was not found", target); 
} 
0

有這樣做的方法很多,一切運作良好...

這裏的你如何能做到一個例子:

final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 }; 
    final int[ ] EMPTY = new int[0]; 
    final int MINIMUM = 0; 
    final int MAXIMUM = 16; 
    int foundIndex; 
    int target; 

    System.out.println("Searching for numbers in an array."); 
    for (target = MINIMUM; target <= MAXIMUM; target++) 
    { 
     foundIndex = -1; 
     System.out.print("\nIs " + target + " in the array? "); 
     { 
      for (int index = 0; index < DATA.length; index++) 
      { 
       if (DATA[index] == target){ 
        foundIndex = index; 
        break; 
       } 
      } 
     } 
     if(foundIndex > -1) 
      System.out.printf("Yes! %d was found at index [%d]", target, foundIndex); 
     else 
      System.out.printf("No! %d was not found", target); 
    } 

另一種方法是使用一個布爾值,如果被發現設置:

final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 }; 
    final int[ ] EMPTY = new int[0]; 
    final int MINIMUM = 0; 
    final int MAXIMUM = 16; 
    boolean found; 
    int target; 

    System.out.println("Searching for numbers in an array."); 
    for (target = MINIMUM; target <= MAXIMUM; target++) 
    { 
     found = false; 
     System.out.print("\nIs " + target + " in the array? "); 
     { 
      for (int index = 0; index < DATA.length; index++) 
      { 
       if (DATA[index] == target){ 
        found = true; 
        System.out.printf("Yes! %d was found at index [%d]", target, index); 
        break; 
       } 
      } 
     } 
     if(!found) 
      System.out.printf("No! %d was not found", target); 
    } 
+0

不錯,謝謝!其實,我想將一些類型的布爾值的,但不知道如何:) – Walby

+0

@Walby你應該接受基於該解決方案的時間正確的答案,如果有人回答你應該在計數之前。正如我看到我的解決方案完全一樣,不同的是我在EderMartins之前回答了。 – DiegoS

+0

兄弟沒問題!編碼就是分享經驗,沒有人會一個人去。希望你做出很好的應用! –

相關問題