2015-01-12 81 views
0

我正在寫一個程序,它讀取包含匹配結果的文本文件,然後應該在表中輸出它們。我有一個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循環,但對我來說,似乎是最合乎邏輯的。有一些對象沒有在這裏聲明 - 這只是代碼片段,省略了聲明,因爲我知道它們的工作原理,並且聲明瞭很多。

+0

X從第一個循環的增量在哪裏? –

+0

已添加 - 必須已將該代碼剪下來,同時整理要在此處發佈的代碼!謝謝! –

+0

請確保您的大括號正確,並且您問題中的大括號與您的代碼中的大括號相匹配。看起來他們可能是錯的,但是如果實際代碼中的大括號與我們所看到的不同,那麼很難分辨出發生了什麼。 – ajb

回答

0

你的問題不清楚。但讓我們指出你提供的代碼

1)你的if和else if語句有什麼區別?他們正在檢查完全相同的東西

if (userteam.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 (userteam.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; 
} 

2)你在做什麼變量x,我沒有看到你在增加它的任何地方。

3)在第一次運行時,當x < = y時,內部循環將完成讀取所有行,因此即使增加了X點,從第二次運行開始內部循環也不會執行。因爲它已經完成閱讀所有行。所以沒有必要這樣做

如果你想提供更多的內容,可以使用示例文本文件數據,這可能有助於回答你的問題。

謝謝。

+0

是的 - 編輯IF ELSE現在有意義 –

0

你的格式在這裏對你不利;正確縮進,你的代碼結構是這樣的(注意,我不得不添加缺失的大括號,},在你提供的代碼的末尾,因爲我假設你在複製代碼時錯過了它們):

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 

     /* stuff */ 

     if (split.length == 4) { 

      /* stuff */ 

      x++; 
      i++; 
     } 
    } 
} 

x和i的增量嵌套在if (split.length == 4) {之內,這意味着x和i只會在該特定情況下遞增,而不會在內部while循環的每次迭代結束時遞增。

+0

我已經改變了大括號的位置 - 我會給它一個現在括號是正確的。現在X和I都增加了IF,他們現在應該增加? –

+0

是的。如果你想在循環的每次迭代結束時完成一些事情,你不應該在條件內嵌套你想做的事情。 – mgomov

+0

@the_kirbinator確保你知道應該增加哪個循環。如果外循環應該運行於'x' = 0,1,2,3,...,那麼你可能不想增加它每次運行內部循環。但是,我並不知道你想要做什麼。 – ajb

1

如果您擔心增量失敗,最好使用For循環。

而是有一段時間(X < y)和在你的代碼某處堅持增量語句, 一個

for (i = 0; i < y; i++) { // do tests here } 

循環將保證你總是遞增,並且運行下一個團隊的考驗。

爲了將來的參考,當使用while循環並遞增時,增量幾乎總是在while循環的END處完成,而不是在兩者之間。增量語句也應該幾乎不會出現在條件語句中(這可能會導致無限循環)。