lundi 7 septembre 2020

Value of Variable Keeps Resetting Every Time The Loop Runs (Java)

I'm currently working on a homework assignment for my Java OOP class. The assignment is that a mouse is trapped midway in a 30ft long pipe. The mouse needs to move randomly with a 50% chance of going left or right. The mouse can only move a random distance between a min of 3ft and a max distance of 8ft for each move made. The program should keep track of the distance that the mouse is from the left and right ends of the pipe after each move and once it has reached one end of the pipe, the loop should end and it should print out that the mouse has reached whatever end it reached. I'm using the variable fromendL to keep track of the mouse's distance from the left end and fromendR for the other end. The problem i'm encountering is that every time the loop runs it's resetting the values of each variable so and it's never giving me the distance over the course of multiple loops. Everything I believe I have working fine, although it might not be the most efficient way (i'm still fairly new to programming).

I'll attach the code below:

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!");
        }
      }




    }

  }
}



Aucun commentaire:

Enregistrer un commentaire