简体   繁体   中英

While/ Do While loops in Java

Here is my problem:

A student has decided to celebrate the start of Thanksgiving break a tad early by getting a bit smashed.

Her home is at corner 0 on Main Street, and the Jail is at corner 10 on Main Street. The student starts at corner 5, and wanders one corner to the left or to the right with probability 0.5 for each; she repeats the process until arriving safely at home or landing in jail. That is, at each corner, the drunk student has a 50-50 probability of staggering left or right, to the next higher-numbered or next lower-numbered corner.

Write a method named drunkWalk() using a while or a do-while loop that simulates the drunk student's walk; your method should return with a single integer that both indicates how many steps were taken and whether the student landed in jail or at home. You can do this if you return the number of steps taken as a negative number if the person lands in jail, and as a positive number if the person ends up at home. You should not print out each step taken in your final version of the method, though you might want to do this while you are debugging.

Once you have your method working, have your main program call upon your drunkWalk() method N times (where N is a final variable). Finally, have it calculate and print the average number of steps that the student took for one trip. Here's what your program might look like in action, with N equal to 5:

Here we go again... time for a walk! <br/>
Took 37 steps, and <br/>
Landed at HOME<br/>

Here we go again... time for a walk!<br/>    
Took 19 steps, and <br/>
Landed in JAIL<br/>

Here we go again... time for a walk!<br/>
Took 13 steps, and <br/>
Landed in JAIL<br/>

Here we go again... time for a walk! <br/>
Took 25 steps, and <br/>
Landed in JAIL<br/>

Here we go again... time for a walk!<br/>
Took 15 steps, and <br/>
Landed at HOME<br/>

Average # of steps equals 25.4<br/>

Here is my code: It compiles but I get no output. I think my code is overcomplicated, but I don't know how to simplify it:

class Drunk     
{

// When this code runs, nothing prints out????

  public static void main(String[] args)
  {
    int home = 0; 
    int jail = 10;  

    final int N = 5; 
    int sum = 0;

    int a = drunkWalk(N); 
    sum = sum + a; 

    //System.out.println ("Average # of steps equals" + " " + (a/sum));

  }

  static int drunkWalk(int x)
  {
    int steps; // steps is the number of steps taken by the student
    int total=5;  // total is the starting point of the student (at corner 5)

    while (x > 0); 
    {   
      do 
      { 
        steps = (int) (Math.random() * (10 +  10) - 10); 
        total += steps;
        x = x-1;
      } while (total != 0 || total != 10);
    }

    if (total == 0) 
    {
      System.out.println("Here we go again... time for a walk!");
      System.out.println("Took" + " " + steps + "steps, and");
      System.out.println ("Landed at HOME"); 
    }

    if (total == 10) 
    { 
      System.out.println("Here we go again... time for a walk!");
      System.out.println("Took" + " " + steps + "steps, and");
      System.out.println ("Landed in JAIL"); 
    }

    return steps; // Need to figure out how to add up ALL the steps
  }

Please help!

; after while terminates your loop. So, remove the semicolon ; after the while statement:

while (x > 0);// remove semicolon  ;

As your first while loop terminated, the value total , steps don't get changed.

Hey your code has an INFINITE loop

do { 
    steps = (int) (Math.random() * (10 +  10) - 10); 
    total += steps;
    x = x-1;
} while (total != 0 || total != 10);

Here suppose your total = 0 then total != 10 gets satisfied so loop will be continued and in case your total = 10 then total != 0 gets satisfied again loop continued "YOU HAVE USED || OPERATOR" . You should use && like this

    do { 
       steps = (int) (Math.random() * (10 +  10) - 10); 
       total += steps;
       x = x-1;
    } while (total != 0 && total != 10);

NOTE : Considering your logic of loop is as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM