简体   繁体   English

如何在while循环中添加距离?

[英]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. 我应该在N步之后加上步行者位置的距离。 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. 我运行了一段时间的T次,需要在T步之后添加所有N个步骤,然后除以尝试次数即可得出平均值。 This is my code so far. 到目前为止,这是我的代码。 Iv tried doing a different integer like distance/T but it says that distance isn't found. iv尝试做一个不同的整数,例如distance / T,但是它说找不到距离。 Is that because it is defined in the while loop. 是因为它是在while循环中定义的。 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 = ");

I'm assuming it's because you're not keeping track of total distance. 我认为这是因为您没有跟踪总距离。 Also, your distance int variable only exists in the scope: 另外,您的distance int变量仅存在于范围内:

        if (N==x)
        {
            int distance=(stepsX*stepsX)+(stepsY*stepsY);
            System.out.println(distance);
        }

Read more about scopes here: Scope of do-while loop? 在此处阅读有关范围的更多信息: do-while循环的范围?

It's kind of unclear what you need, but here is some changes I made that I think will help: 尚不清楚您需要什么,但是我做了一些我认为会有所帮助的更改:

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;
double totalDistance = 0.0;//keep track over over distance;
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);
            totalDistance = totalDistance + distance;
            System.out.println("current distance:  " + distance);
            System.out.println("current total distance:  " + totalDistance);
        }
    }
    T=T+1;
    N=0;
}
//calculate whatever you need using totalDistance
System.out.println("mean sq. dist = " + (totalDistance/T) );

This would be my answer, you seem to be loosing all of your steps except last one in a while loop. 这将是我的答案,您似乎正在失去所有步骤,除了while循环中的最后一个步骤。

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;
int stepsX = 0;
int stepsY = 0;
while (T<10)
{
    while (N<x)
    {
        stepsX += Math.round(random(1,10));
        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;
}
double distance = Math.sqrt(Math.pow(stepsX, 2) + Math.pow(stepsY, 2));
System.out.println("mean sq. dist = "+distance);

But please if it is not rephrase your question. 但是请不要重新表达您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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