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");
     }

  }

}

你好,以上是我目前的程序。我在让它提取所有相应变量时遇到问题。IE:我输入“200”,数组中有 (2) 个单元具有相应的 (200) 值,但是,这只会打印出其中的 1 个。

任何人有任何快速指针?

4

1 回答 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");
 }
于 2013-07-18T21:43:44.870 回答