我正在介绍 CS 课程,我必须从文件中提取字符串并打印该字符串在文件中的次数。它的那部分工作正常。问题是我们必须循环它以让它通过相同的过程处理尽可能多的字符串。当我将计算字符串在文件中的次数的变量计数器重置为 0 时,输出显示该变量为 0。它被初始化为 0,所以我看不到循环后发生了什么变化。
while (answer) {
int timesUsed = 0; for (int i = 0; i < monAr.length; ++i) {
while (monFile.hasNext()) {
monAr[i] = monFile.next();
if (monAr[i].equalsIgnoreCase(desiredTag)) {
timesUsed = timesUsed + 1;
}
}
}
System.out.println("On Monday, #" + desiredTag + " appeared " + timesUsed + " times "+ "and was " + (((float) timesUsed / monAr.length) * 100) + "% of all hashtags used for the day.");
System.out.print("Do you want to search another hashtag (y/n)? ");
choice = scnr.nextLine();
if (choice.equals("n")) {
answer = false;
}
}