我被要求檢查在我的計算機上創建的文本文件中是否存在團隊名稱。我編寫了完整的代碼,但輸出總是在我計算團隊名稱出現在文件中的次數之前輸入團隊名稱兩次。請看看這個,讓我知道。謝謝。調用鍵入球隊名稱兩次而不是一次
import java.util.*;
import java.io.*;
public class worldSeries
{
public String getName(String teamName)
{
Scanner keyboard = new Scanner(System.in);
System.out.println(" Enter the Team Name : ");
teamName = keyboard.nextLine();
return teamName;
}
public int checkSeries1() throws IOException
{
String teamName="";
String[] winners = new String[50];
int i = 0 ;
File file = new File ("WorldSeriesWinners.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext() && i < winners.length)
{
winners[i] = inputFile.nextLine();
i++;
}
inputFile.close();
int count = 0;
String nameOfTeam = getName(teamName);
for (int index = 0 ; index < winners.length ; index ++)
{
if (nameOfTeam.equals(winners[index]))
{
count++;
}
}
return count;
}
public static void main(String[]Args)
{
String teamName = "";
worldSeries object1 = new worldSeries();
try
{
System.out.println(" The Number of times " + object1.getName(teamName) + "won the Championship is : " +object1.checkSeries1());
}
catch (IOException ioe)
{
System.out.println(" Exception!!! ");
ioe.printStackTrace();
}
}
}
你爲什麼要張貼[類似的問題(HTTP://計算器.com/questions/22119883/again-and-again-prints-the-same-value)但是[不同名稱](http://stackoverflow.com/users/3339936/user3339936)? –
只需在'getName()'方法中放置一個斷點,然後查看它何時被調用,然後使用調用堆棧(或僅僅跳出它)來查看它從哪裏調用。 – hyde