2013-07-18 15 views
0
void searchForPopulationChange() 
    { 
    int input; 
    int searchCount = 0; 

    System.out.println ("Enter the Number for Population Change to be found: "); 
    input = scan.nextInt(); 
    boolean found = false; 
    for (searchCount = 0; searchCount < populationChange.length; searchCount++) 
    { 

     if (populationChange[searchCount] == input) 
     { 
      found = true; 
      break; 
     } 
    } 
    if (found) 
    { 
     System.out.print(""+countyNames[searchCount]+" County/City with a population of "+populationChange[searchCount]+" individuals\n"); 
    } 
    else 
    { 

     System.out.print("WRONG INPUT"); 
    } 

    } 

}搜索specfic數的數組,並打印出所有相應的變量

你好,上述目前我的節目。 我有一個問題,就是讓它拉出所有相應的變量。 IE:我輸入「200」,數組中有(2)個單位具有相應的(200)值,但是,這隻打印出其中的1個單位。

任何人有任何快速指針?

回答

2

,而不是破壞,當你發現你的價值

for (searchCount = 0; searchCount < populationChange.length; searchCount++) 
{ 

    if (populationChange[searchCount] == input) 
    { 
     found = true; 
     break; 
    } 
} 
if (found) 
{ 
    System.out.print(""+countyNames[searchCount]+" County/City with a population of "+populationChange[searchCount]+" individuals\n"); 
} 

只是打印當場

for (searchCount = 0; searchCount < populationChange.length; searchCount++) 
{ 

    if (populationChange[searchCount] == input) 
    { 
     found = true; 
     System.out.print(""+countyNames[searchCount]+" County/City with a population of "+populationChange[searchCount]+" individuals\n"); 
    } 
} 

你仍然可以檢查錯誤輸入你的循環結束後

if (found == false) 
{ 
    System.out.print("WRONG INPUT"); 
} 
+0

這樣做會讓我將else語句移回for循環 ,然後我的輸出顯示2 (找到)變量,但是,然後還包括一個巨大的垃圾郵件的「錯誤的輸入」錯誤輸入「 – user2517185

+0

即時通訊不知道如何重新發布我的代碼 - ;;; – user2517185

+0

@ user2517185我編輯了我的答案。如果你需要重新發布代碼,你仍然可以檢查'found'的值' –