1
我想顯示迄今爲止播放的遊戲的結果,並且我有3個文本文件(results
,fixtures
和teamsOrPlayers
)。我希望能夠給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: }
你現在遇到什麼問題? – suspectus 2013-03-20 00:20:57
我不確定如何將項目存儲到數組....我得到了最後一位代碼(最後的循環向前)關閉一個朋友,他說這將工作,如果我可以將數據存儲在一個數組 – 2013-03-20 00:29:32
這是整個程序還是其中的一部分? – 2013-03-20 00:40:10