【问题标题】:How do I add the distances in a while loop?如何在while循环中添加距离?
【发布时间】:2013-09-11 19:18:56
【问题描述】:

我应该在 N 步后添加步行者位置的距离。我运行了 while T 次,需要在 T 跟踪之后添加所有 N 个步骤,然后除以试验次数以获得平均值。到目前为止,这是我的代码。我尝试做一个不同的整数,比如距离/T,但它说没有找到距离。那是因为它是在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;
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 = ");

【问题讨论】:

  • 要做的第一件事:修复缩进。如果您的代码在您使用的任何编辑器中真的都是这样,那会让您的生活变得更加艰难。如果它实际上在编辑器中正确缩进,但您在这里没有费心这样做 - 那会让 我们的 生活变得更加艰难。
  • 我编辑了你的噩梦代码,但你介意改写你的问题吗?
  • 是的,花时间制作好的压痕。大多数编辑会帮助你。当您在此处粘贴某些内容时,请注意检查是否保留了缩进。这可能比正确使用指针符号更重要。
  • 是的,我为我的缩进道歉。我对编程真的很陌生,所以我还在学习这种格式。当N=x(即int距离)时,我不知道如何添加每条路径的距离。我需要从 T 的每条轨迹添加每个距离。
  • @user2770061 什么是 T 的“轨迹”?

标签: java while-loop


【解决方案1】:

我假设这是因为您没有跟踪总距离。此外,您的 distance int 变量仅存在于范围内:

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

在此处阅读有关范围的更多信息:Scope of do-while loop?

有点不清楚您需要什么,但我做了一些我认为会有所帮助的更改:

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) );

【讨论】:

  • 这很有帮助。我只需要添加一个不同的 int 就像你在这段代码中的样子。对困惑感到抱歉。我会尽量让我的符号更精确。
【解决方案2】:

这将是我的答案,您似乎正在丢失所有步骤,除了 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);

但是,如果不是,请改写您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 2017-06-21
    • 2019-12-06
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多