每次循环运行时变量的值都会不断重置(java)

bmvo0sr5  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(507)

我目前正在为我的javaoop课程做家庭作业。任务是一只老鼠被困在30英尺长的管道中间。鼠标需要随机移动,向左或向右移动的概率为50%。对于每次移动,鼠标只能在最小3英尺和最大8英尺之间随机移动距离。程序应该在每次移动后跟踪鼠标与管道左右两端的距离,一旦鼠标到达管道的一端,循环就应该结束,并且应该打印出鼠标已经到达了它到达的任何一端。我使用变量fromendl来跟踪鼠标从左端到另一端的距离。我遇到的问题是,每次循环运行时,它都会重置每个变量的值,因此它永远不会给出多个循环过程中的距离。我相信我所做的一切都很好,尽管这可能不是最有效的方法(我对编程还是相当陌生)。
我将在下面附上代码:

class Main {
  public static void main(String[] args)
  {
    int fromendL;
    int fromendR;
    int tMin = 3;
    int tMax = 8;
    int direction;
    int travel;
    int travelR = 15;
    int travelL = 15;
    boolean escaped = false;

    while (!escaped)
    {
      direction = (int)(Math.random()* 2 + 1);
      travel = (int)(Math.random() * (tMax - tMin) + 3);

      if (direction == 1)
      {
        fromendL = 15 - travel;
        fromendR = 30 - 15 + travel;
        travelL = travelL - travel;

        System.out.println("The mouse has moved: " + travel + " ft to the left" );
        System.out.println("The mouse is " + fromendL +" ft from The Left End");
        System.out.println("The mouse is " + fromendR +" ft from The Right End\n");

        if(travelL == 0 || travelL < 0)
        {
          escaped = true;
          System.out.println("The Mouse has escaped the Left end of the Pipe!");

        }

        else if (travelR == 30 || travelR > 30)
        {
          escaped = true;
          System.out.println("The Mouse has escaped the Right end of the Pipe!");
        }
      }

      else if (direction == 2)
      {
        fromendL = 15 + travel;
        fromendR = 30 - 15 - travel;
        travelR = travelR + travel; 
        System.out.println("The mouse has moved: " + travel + " ft to the right");
        System.out.println("The mouse is " + fromendL +" ft from The Left End");
        System.out.println("The mouse is " + fromendR +" ft from The Right End\n");

        if(travelR == 30 || travelR > 30)
          {
            escaped = true;
            System.out.println("The Mouse has escaped the Right end of the Pipe!");

          }

        else if (travelL == 0 || travelL < 0)
        {
          escaped = true;
          System.out.println("The Mouse has escaped the Left end of the Pipe!");
        }
      }

    }

  }
}
blpfk2vs

blpfk2vs1#

下面的代码应该可以解决原来的问题。请对它进行分析,找出与代码相比的一些改进。
而不是用两个变量来保持位置( fromendL 以及 fromendR ),我用一个( pos ). 如果我知道管道的长度(不变)和从管道左侧开始的位置,就可以计算到右端的距离。
构造像 if (a == 0 || a < 0) 最好是这样写的 if (a <= 0) 在我看来。
我使用方向为+1或-1。就像在数学中,负方向就像向左,正方向就像向右。
每次重置变量的原因在于

fromendL = 15 - travel;
    fromendR = 30 - 15 + travel;
    travelL = travelL - travel;

你可以从15到30,反复地开始,而不是使用鼠标的最后一个位置。
我的代码看起来像(我使用制表符进行缩进;我的眼睛太老了,看不到细微的两个字符的凹痕;):

class Main {

    public static void main(String[] args)
    {
        static final int tMin = 3;
        static final int tMax = 8;
        static final int pLength = 30;
        int direction;
        int travel;
        int pos = pLength / 2;
        boolean escaped = false;

        while (!escaped) {
            direction = (int)(Math.random() * 2);
            if (direction == 0) direction = -1;
            travel = ((int)(Math.random() * (tMax - tMin)) + tMin) * direction;
            pos += travel;
            System.out.println("The mouse has moved: " + Math.abs(travel) + " ft to the " + (travel > 0 ? "right" : "left"));
            System.out.println("The mouse is " + pos +" ft from The Left End");
            System.out.println("The mouse is " + (pLength - pos) + " ft from The Right End\n");

            if (pos >= pLength || pos <= 0) {
                System.out.println("The Mouse has escaped the " +  (pos <= 0 ? "Left" : "Right") + " end of the Pipe!");
                escaped = true;
            }
        }
    }
}

这个 ? 是三元运算符。它测试条件,如果为真,表达式的计算结果将直接跟在问号后面。如果为false,则表达式的计算结果为冒号后面的值。小心使用,很多人不喜欢,觉得很混乱。出于这个原因,我把它放在括号之间,这样就可以清楚地知道构造的开始和结束。
因为pos要么小于或等于零(转义到左边),要么大于或等于30(转义到右边),所以我只需要测试两者中的哪一个是。如果我知道老鼠没有逃到左边,那它一定是逃到右边了(环境状况 if 已经保证老鼠逃走了)。

相关问题