自从制作了一些代码之后,我对某个部分之后的操作有点困惑。它应该通过ArrayList放置一个文本文件,然后用户应该能够键入一个团队名称,它应该添加它们在文本文件中弹出的次数。但是对于我的生活,我仍然坚持如何继续我的生活。
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> mlb = new ArrayList<String>();
Scanner fileReader = new Scanner(new File("WorldSeriesWinners.txt")); // Declares the scanner to read from the file.
int Count = 0;
Scanner keyboard = new Scanner(System.in);
String userInput;
while (fileReader.hasNext()){
// While loop to continue to read while there is another line.
mlb.add(fileReader.nextLine());
}
System.out.println("Enter a championship team you are looking up(1903-2009).");
userInput = keyboard.nextLine();
for(String team : mlb){ // Will run through the file and display the ArrayList.
if(team.contains(userInput))
System.out.println(team);
答案 0 :(得分:1)
将userInput出现在ArrayList中的次数加起来。
int count = 0;
for(String team : mlb){ // Will run through the file and display the ArrayList.
if(team.contains(userInput))
count++;
}
System.out.println(count);
答案 1 :(得分:0)