簡體   English   中英

While循環條件問題

[英]Issues with While loop condition

我正在編寫一個程序,該程序讀取包含匹配結果的文本文件,然后將其輸出到表中。 我在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次。

內部循環將繼續,同時提供的文件有下一行。 文本文件有時可能比其他文件具有更多行的結果。 在此循環中,我有一個對Array項的引用:

if (userteam.equals(Teams.get(i)))

在第一次運行中,這將指向我的Array中的0,據記錄,該值為Leeds Rhinos。 內循環完成后,便移至外循環-處理剛剛記錄的結果。 如果當前團隊是利茲犀牛隊,則應添加值。 然后,i應該添加1,因此對於下一個循環,它引用數組的索引1,而不是0。(我在這里有更多IF語句,所有語句都相同,但引用其他團隊)變量設置回0 ,准備下一次運行。

這個問題我有,是, 似乎並未有1每次貫穿時添加的,所以我只得到過一個團隊傳遞的結果。 如果我手動指定要查看的數組索引(例如3),它將遍歷整個數組,並且團隊將成功記錄其結果。

有沒有一種方法可以讓每次循環加1? 我不確定這是否是正確的Java循環,但對我來說似乎是最合邏輯的。 這里有一些未聲明的對象-這只是代碼的一部分,據我所知,它們省略了聲明,並且有很多聲明。

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

而不是花一會兒(x <y)並在代碼中的某個位置添加一個增量語句,而是

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

循環將確保您始終遞增並為下一個團隊運行測試。

供以后參考,當使用while循環並遞增時,遞增幾乎總是在while循環的END處進行,而不是在兩者之間進行。 增量語句也應該幾乎永遠不要在條件語句中(這可能會導致無限循環)。

您的問題不清楚。 但讓我們指出您提供的代碼中的某些內容

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增加一點,從第二次運行開始,內部循環也不會執行。 由於它已經讀完所有行。 所以這樣做沒有意義

同樣,如果您提供了更多關於想要完成的內容的信息,則可能是示例文本文件數據,這可能有助於回答您的問題。

謝謝。

您的格式在這里對您不利; 適當地縮進,您的代碼結構是這樣的(注意,我必須在提供的代碼末尾添加丟失的右花括號} ,因為我假設您在復制代碼時就錯過了它們):

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循環的每次迭代結束時遞增。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM