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循环结束时, 如果他希望再次执行程序,我需要它来提示用户。 是还是不是? 我得到提示打印 但是,扫描功能不起作用。 有什么建议吗?
答案 0 :(得分:7)
从
中删除分号if (response.equalsIgnoreCase("yes"));
和
if (response.equalsIgnoreCase("no"));
答案 1 :(得分:1)
你的另一个问题(在if
之后的分号旁边)将是nextLine
在nextInt
之后使用不会消耗新线标的事实,所以首先{{1}将返回空字符串。要摆脱它,试试
nextLine
或
input = scan.nextInt();
scan.nextLine();//to consume new line mark