2013-03-20 154 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。另外,您不需要在变量名称中指定变量的类型。查找“反向波兰表示法”,你会看到如何在名称中放置类型通常是代码维护问题。
相关问题