Wednesday, 11 September 2013

How do I add the distances in a while loop?

How do I add the distances in a while loop?

I am supposed to add the distance of a walker's position after N steps. I
run the while T times and need to add all of the N steps after T trails
and divide by the amount of trials to get an average. This is my code so
far. Iv tried doing a different integer like distance/T but it says that
distance isn't found. Is that because it is defined in the while loop. I
am using processing.
import javax.swing.JOptionPane;
String input=JOptionPane.showInputDialog("Steps");
int x=Integer.parseInt(input);
if (x<0)
{
System.out.println("Error. Invalid Entry.");
}
int T=1;
int N=0;
while (T<10)
{
while (N<x)
{
int stepsX=Math.round(random(1,10));
int stepsY=Math.round(random(1,10));
System.out.println(stepsX+","+stepsY);
N=N+1;
if (N==x)
{
int distance=(stepsX*stepsX)+(stepsY*stepsY);
System.out.println(distance);
}
}
T=T+1;
N=0;
}
System.out.println("mean sq. dist = ");

No comments:

Post a Comment