2013-03-20 148 views
1

我想顯示迄今爲止播放的遊戲的結果,並且我有3個文本文件(resultsfixturesteamsOrPlayers)。我希望能夠給teamNames讀入一個數組,然後能夠描繪結果一樣(阿森納2:1曼城)將文本文件信息存儲到數組

1: import java.io.*; 
2: import java.util.*; 
3: import javax.swing.JOptionPane; 
4: 
5: public class Text3 
6: { 
7:  public static void main(String args[]) 
8:  { 
9: 
10:   // Declaring the text files 
11:   File results = new File ("PremiershipResults.txt"); 
12:   File fixtures = new File ("PremiershipFixtures.txt"); 
13:   File teamsOrPlayers = new File("PremiershipTeamsOrPlayers.txt"); 
14: 
15:   String lineFromFile ; 
16: 
17:   //Decalring 2 arrays to store the fixtures and results in 
18:     int fixturesArray [] ; 
19:   int resultsArray [] ; 
20: 
21:     //Im not sure whether these are needed just something i found on the        internet 
22:     int count = 0 , teamsCount = 0 , teamNumber; 


23:   //This is stating how many teams there are and adding 1 to count everytime there is a team 

24:     Scanner input = new Scanner (teamOrPlayers) 
25:   while (input.hasNext()) 
26:   { 
27:    input.nextLine(); 
28:    count++; 
29:   } 
30:   input.close(); 
31: 
32:   String teamNames [] = new String [count] ; 
33:   Scanner input = new Scanner (teamsOrPlayers); 
34:   while (input.hasNext()) 
35:   { 
36:    lineFromFile = input.nextLine(); 
37:    //The text files are seperated by commas eg. Results file would be as follows - 1,2,3 and this means fixture 1 and the result is 2-3 
38: 
39:    teamsArray = lineFromFile.split(",") ; 
40: 
41: 
42: 
43:   //This is the code i got off a friend and he said it would work if i can store the info into arrays 

44: 
45: 
46:    for(int i = 0; i < results.get(0).size(); i++) 
47:    { 
48:    int homeTeam = Integer.parseInt(fixtures.get(1).get(i)); 
49:    int awayTeam = Integer.parseInt(fixtures.get(2).get(i)); 
50:    String homeTeamStr = teamsOrPlayers.get(1).get(homeTeam - 1); 
51:    String awayTeamStr = teamsOrPlayers.get(1).get(awayTeam - 1); 
52: 
53:    int homeResult = Integer.parseInt(results.get(1).get(i)); 
54:    int awayResult = Integer.parseInt(results.get(2).get(i)); 
55: 
56:    System.out.printf("%s %s - %s %s\n", homeTeamStr, homeResult, awayResult,  awayTeamStr); 
57:   } 
58:  } 
59:  } 
60: } 
+4

你現在遇到什麼問題? – suspectus 2013-03-20 00:20:57

+0

我不確定如何將項目存儲到數組....我得到了最後一位代碼(最後的循環向前)關閉一個朋友,他說這將工作,如果我可以將數據存儲在一個數組 – 2013-03-20 00:29:32

+0

這是整個程序還是其中的一部分? – 2013-03-20 00:40:10

回答

1

代碼審查意見....

  • 你縮進不一致。使其一致將使閱讀代碼變得更加容易。這個標準並不特別重要,但是你應該有一個標準,但是我建議在編寫Java代碼時遵循Java標準。
  • 就像寫散文一樣,空格很重要。組合代碼將相似的東西組合在一起,並使用換行符將它們分開,就像使用段落一樣。這將使您的代碼更容易閱讀 - 無論是爲了您還是爲其他人。評論和他們評論的代碼之間通常不應該有空行。
  • 您在第25行有一個循環來預處理文件並找出您有多少行。我懷疑你是這樣做的,因爲你正在使用數組,並且你還沒有學習像Vector這樣的類。對於家庭作業而言,這並不重要,但如果這是產生數千次的代碼,它可能會變成瓶頸。您最終將被教授如何編寫高效的代碼,因此請考慮此評論對該主題的簡要介紹。
  • 命名您的變量來表示它們真正包含的內容。例如,teamsArray似乎包含來自PermierShipTeamsOrPlayers.txt的一行,這似乎代表單個遊戲的結果。命名爲「teamsArray」意味着它包含一系列不同團隊的陣列 - 列表。更好的名字是gameResult。另外,您不需要在變量名稱中指定變量的類型。查找「反向波蘭表示法」,你會看到如何在名稱中放置類型通常是代碼維護問題。
相關問題