0
void searchForPopulationChange()
  {
     String goAgain;
     String response;
     int input;
     int searchCount = 0;
     boolean found = false;
     boolean search = false; 


     while(search == false)
     {

        System.out.println ("Enter the Number for Population Change to be found: ");
        input = scan.nextInt();


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

        }

        if (found == false) {
           System.out.println("No records found!");
        }


        System.out.println("\n\n-------------------------------------------------------------------------");
        System.out.println("\n\tDo you want to enter data for another Course? Type Yes or No: "); 
        response = scan.nextLine(); //the response is already recognized - thus ends or restarts the program      

        if (response.equalsIgnoreCase("yes"));
        {
           search = false;
        }

        if (response.equalsIgnoreCase("no"));
        {
           search = true;
        }      



     }


  }

} 你好!到目前为止,这是我的程序,基本上,当 while 循环结束时,我需要它提示用户是否希望再次执行该程序。是还是不是?我收到打印提示,但是扫描功能不起作用。有什么建议吗?

4

2 回答 2

7

删除分号

if (response.equalsIgnoreCase("yes"));

if (response.equalsIgnoreCase("no"));
于 2013-07-19T02:44:12.890 回答
1

您的另一个问题(除了ifs 之后的分号)将是您在使用nextLine之后nextInt不会消耗新行标记的事实,因此首先nextLine将返回空字符串。要摆脱它,请尝试

input = scan.nextInt();
scan.nextLine();//to consume new line mark

或者

input = Integer.parseInt(scan.nextLine());
于 2013-07-19T02:49:31.210 回答