java - For Loop error as method -
i trying write code
changes value either -1 or +1 depending on random chance. person moving through blocks. starts 6 , if ends in 1 wins if ends in 11 loses. final output this:
here go again... time walk! walked 37 blocks, , landed @ home here go again... time walk! walked 19 blocks, , landed in jail here go again... time walk! walked 13 blocks, , landed in jail here go again... time walk! walked 25 blocks, , landed in jail
i have written following code:
public class drunk { public int street; public double move; public int i; public boolean jail; public static void drunkwalk() { { street = 6; move = math.random(); i++; if (move > 0.5) { street++; } else { street--; } } while (street != 1 && street != 11); if ( street == 1) { jail = false; } else { jail = true; } (; ; ) { --- } //this problem. treats method. //how can fix this? } }
how somethink like:
public static void main(string args[]) { drunk drunk = new drunk(); while (true) { drunkresult result = drunk.drunkwalktojail(); system.out.println("walked " + result.getsteps() + " blocks, , landed @ " + (result.isinjail() ? "jail":"home")); } } public drunkresult drunkwalktojail() { int street; int stepcount = 0; { street = 6; double move = math.random(); stepcount++; if (move > 0.5) { street++; } else { street--; } } while (street != 1 && street != 11); return new drunkresult(street == 11, stepcount); }
and
public class drunkresult { boolean jail = false; int stepcount = 0; public drunkresult(boolean jail, int stepcount) { this.jail = jail; this.stepcount = stepcount; } public boolean isinjail() { return jail; } public int getsteps() { return stepcount; } }
you can walks in parallel (a group of drunk people) , process results independent.
Comments
Post a Comment