我正在寫一個程序,它讀取包含匹配結果的文本文件,然後應該在表中輸出它們。我有一個while循環中While循環:While循環條件的問題
Scanner fileread1 = new Scanner(new File("demo.txt"));
int x = 0;
int y = 22;
int i = 0;
while (x <= y) {
while (fileread1.hasNext()) {
fileinput = fileread1.nextLine(); // this reads the next line of
// from the file
String line = fileinput;
String[] split = line.split(":");
boolean result = false;
int homescore1 = 0;
int awayscore1 = 0;
int goalsscored = 0;
boolean att = false;
boolean htt = false;
int atscore = 0;
int htscore = 0;
// When the text line is read, it is then split into four sections.
if (split.length == 4) {
// String text = line.trim();
String userteam = userteaminput;
String hometeam = split[0].trim();
String awayteam = split[1].trim();
String home_score = split[2].trim();
String away_score = split[3].trim();
// this is a test to try convert the goals string into a
// integer. If this fails, the result is not
// not valid and does not get outputted to the console.
try {
homescore1 = Integer.parseInt(home_score);
awayscore1 = Integer.parseInt(away_score);
result = true;
}
catch (NumberFormatException exception) {
// if the try is not able to convert, this will run
errors++;
}
if (userteam.equals(Teams.get(i))) {
if (awayteam.equalsIgnoreCase(userteam)) {
att = true;
games++;
goalsfor = goalsfor + awayscore1;
goalsagainst = goalsagainst + homescore1;
}
if (att == true && awayscore1 > homescore1) {
atwc++;
gameswon++;
}
else if (att == true && awayscore1 < homescore1) {
htwc++;
gameslost++;
}
else if (att == true && awayscore1 == homescore1) {
gamesdrawn++;
}
if (hometeam.equalsIgnoreCase(userteam)) {
htt = true;
totaluser++;
games++;
goalsfor = goalsfor + homescore1;
goalsagainst = goalsagainst + awayscore1;
}
if (htt == true && homescore1 > awayscore1) {
atwc++;
gameswon++;
}
else if (htt == true && homescore1 < awayscore1) {
htwc++;
gameslost++;
}
else if (htt == true && awayscore1 == homescore1) {
gamesdrawn++;
}
}
}
else {
errors++;
}
}
// ********************************************************************
// Leeds IF Statement
// ********************************************************************
if (Rhinos.equals(Teams.get(i)) {
Rhinos.goalsfor = Rhinos.goalsfor + goalsfor;
Rhinos.gameswon = Rhinos.gameswon + gameswon;
Rhinos.gameslost = Rhinos.gameslost + gameslost;
Rhinos.goalsagainst = Rhinos.goalsagainst;
Rhinos.gamesplayed = Rhinos.gamesplayed + games;
}
else if (Bulls.equals(Teams.get(i)) {
Bulls.goalsfor = Bulls.goalsfor + goalsfor;
Bulls.gameswon = Bulls.gameswon + gameswon;
Bulls.gameslost = Bulls.gameslost + gameslost;
Bulls.goalsagainst = Bulls.goalsagainst;
Bulls.gamesplayed = Bulls.gamesplayed + games;
}
x++;
i++;
goalsfor = 0;
gameswon = 0;
gameslost = 0;
gamesagainst = 0;
}
我知道有永遠只能將是22支球隊在提供的文本文件的結果,所以第一循環應爲22倍運行。
內部循環將繼續,而提供的文件有下一行。該文本文件有時可能有更多的結果線,然後其他文件。在這個循環中,我有一個數組元素的引用:
if (userteam.equals(Teams.get(i)))
在第一次運行,這將參考0我的數組其中,備案,是利茲犀牛。一旦內部循環完成,它就會移到外部循環 - 這將處理剛纔記錄的結果。如果目前的隊伍是利茲犀牛,那麼應該添加這些值。然後我應該添加1,所以對於下一個循環,它指的是數組1的索引,而不是0.(我在這裏有更多的IF語句,全部相同,但是指向其他團隊)變量被設置回0 ,爲下一次運行做好準備。
我的問題是,我似乎沒有每增加1次,所以我只得到一個團隊通過的結果。如果我手動指定要查看的數組索引(比如說3),它將貫穿始終,並且團隊將成功記錄其結果。
有沒有一種方法,我可以得到1加入到我每次它循環?我不確定這是否是正確的Java循環,但對我來說,似乎是最合乎邏輯的。有一些對象沒有在這裏聲明 - 這只是代碼片段,省略了聲明,因爲我知道它們的工作原理,並且聲明瞭很多。
X從第一個循環的增量在哪裏? –
已添加 - 必須已將該代碼剪下來,同時整理要在此處發佈的代碼!謝謝! –
請確保您的大括號正確,並且您問題中的大括號與您的代碼中的大括號相匹配。看起來他們可能是錯的,但是如果實際代碼中的大括號與我們所看到的不同,那麼很難分辨出發生了什麼。 – ajb