简体   繁体   English

在数组中搜索特定数字并打印出所有相应的变量

[英]Searching an array for specfic numbers and printing out all corresponding variables

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

  }

} }

hello, above is currently my program. 你好,上面是我目前的程序。 I am having an issue with having it pull out ALL of the corresponding variables. 我有一个问题,让它拉出所有相应的变量。 IE: I enter "200", there are (2) units in the array that have a corresponding (200) value, however, this only prints out 1 of them. IE:我输入“200”,数组中有(2)个单位具有相应的(200)值,但是,这只打印出其中的1个。

Anyone have any quick pointers? 有没有快速指针?

instead of breaking when you find your value 当你找到自己的价值时,而不是打破

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

just print it on the spot 只需在现场打印即可

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

you can still check for wrong input after your loop is finished 循环结束后,您仍然可以检查输入错误

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM