【问题标题】:Issues with While loop conditionWhile 循环条件的问题
【发布时间】:2015-01-12 20:55:06
【问题描述】:

我正在编写一个程序,它读取包含匹配结果的文本文件,然后将它们输出到表格中。我在 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)))

在第一次运行中,这将引用我的 Array 中的 0,作为记录,它是 Leeds Rhinos。一旦内循环完成,它就会移动到外循环 - 这处理刚刚记录的结果。如果当前团队是 Leeds Rhinos,则应添加值。然后 i 应该添加 1,因此对于下一个循环,它指的是数组 1 的索引,而不是 0。(我这里有更多 IF 语句,所有相同但参考其他团队)变量设置回 0 ,为下一次运行做好准备。

我遇到的问题是,i 似乎每次运行时都没有添加 1,所以我只得到一个团队的结果。如果我手动指定要查看的数组索引(比如 3),它将运行,团队将成功记录他们的结果。

有没有一种方法可以在每次循环时将 1 添加到 i 中?我不确定这是否是要使用的正确 java 循环,但对我来说,这似乎是最合乎逻辑的。这里没有声明一些对象 - 这只是代码的 sn-p,省略了声明,因为我知道它们可以工作,并且声明了很多。

【问题讨论】:

  • 第一个循环中 X 的增量在哪里?!
  • 已添加 - 在整理代码以在此处发布时必须删除它!谢谢!
  • 请确保您的花括号是正确的,并且您问题中的花括号与您的代码中的花括号相匹配。看起来它们可能是错误的,但如果您的实际代码中的大括号与我们看到的不同,就很难判断发生了什么。
  • 我不在乎别人怎么说:埃及花括号很难阅读。这不是带有 100-500mb 硬盘的 80 年代/90 年代。硬盘现在很大。无需通过在 if 语句和循环的开头保留 { 之前的换行符来节省空间。
  • @developerwjk 硬盘大小不是最大的因素;最大的限制是屏幕上可以容纳多少行。由于行数有限,我的偏好是不要用太多的行来保存{;我宁愿看到更多的逻辑而不必滚动。再说一次,当它们有帮助时,我仍然在我的代码中添加空行。这是一种权衡,在哪里取得平衡取决于个人喜好。

标签: java loops while-loop nested-loops


【解决方案1】:

如果您担心增量失败,最好使用 For 循环。

而不是有一段时间 (x

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

循环将保证您始终为下一个团队增加和运行测试。

为了将来参考,当使用 while 循环和递增时,递增几乎总是在 while 循环的 END 完成,而不是介于两者之间。增量语句也应该几乎永远不会出现在条件语句中(这可能会导致无限循环)。

【讨论】:

    【解决方案2】:

    你的问题不清楚。但是让我们在您提供的代码中指出一些内容

    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

    再次,如果您提供更多关于您想要完成的内容的信息,可能是示例文本文件数据,这可能有助于回答您的问题。

    谢谢。

    【讨论】:

      【解决方案3】:

      您的格式在这里对您不利;正确缩进,您的代码结构是这样的(注意,我必须在您提供的代码末尾添加缺少的右大括号},因为我假设您在复制代码时错过了它们):

      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 循环的每次迭代结束时递增。

      【讨论】:

      • 我已经改变了大括号的位置 - 现在我会试一试,现在大括号是正确的。既然 X 和 I 从 IF 中增加了,它们现在应该增加吗?
      • 是的。如果您想在循环的每次迭代结束时完成某些事情,则不应将您想要完成的事情嵌套在条件中。
      • @the_kirbinator 确保你知道它们应该在哪个循环中递增。如果应该为x=0、1、2、3、...运行外部循环,那么你可能不想在每次内部循环运行时增加它。但是,我真的不知道您要做什么。
      • @the_kirbinator 如果此答案解决了您的问题,请考虑接受:Why?How?
      • 好的,所以我有了它,所以每次运行时我都会递增。我现在遇到的问题是我将 i 用于某些 IF 语句。 IF 语句似乎只在我在 .get() 中输入整数时才起作用。我确实正确地增加了 - 有什么想法吗?
      猜你喜欢
      • 2020-09-17
      • 2011-06-19
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多