Correct me if i am wrong, My understanding is that you want to run the walk cycle a certain number of times and calculate the average distance walked on the sum of the distance of the cycles. If that is the case, then all you have to do is this,
int noc = Integer.valueOf(JOptionPane.showInputDialog("Enter # of cycles: "));
String a = JOptionPane.showInputDialog("Enter # of footsteps.");
int z = Integer.valueOf(a);
int sum = 0;
double avg = 0.0;
for(int i=0;i<noc;i++) {
sum+= randomWalk(z);
}
avg=(double)sum/noc;
System.out.println("the average distance walked in "+ noc + "cycles is "+avg);
the randomWalk()
method should be like the following if you are calling it from the main method without creating an object for the class randomWalk()
is residing in.
public static int randomWalk(int z) {
//place your code here, starting from the `int x=0;`
//at last instead of printing the distance walked use the following code
return (int) Math.round(Math.sqrt((x*x)+(y*y)));
}
you have also missed to call the methods round()
and sqrt()
using there class Math
. I have correct them for you as Math.round()
and Math.sqrt()
. without the class name you will get a compiler error like Symbol not found
. i also assume you have imported the java.lang.Math
class into your program.