gpt4 book ai didi

java - 初学者,我遇到了嵌套 while 循环问题

转载 作者:行者123 更新时间:2023-11-30 04:00:05 25 4
gpt4 key购买 nike

所以我们的作业有两个部分。这就是该部分的内容。

A drunkard begins walking aimlessly, starting at a lamp post. At each time step, the drunkard forgets where he or she is, and takes one step at random, either north, east, south, or west, with probability 25%. How far will the drunkard be from the lamp post after N steps?

Write a program RandomWalker.java that takes an integer command-line argument N and simulates the motion of a random walker for N steps. After each step, print the location of the random walker, treating the lamp post as the origin (0, 0). Also, print the square of the final distance from the origin.

这个程序运行正常,这是我的代码。

package project2;

import java.util.*;
import java.math.*;

public class RandomWalker {
public static void main(String args[]){

Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Enter the number of steps you want to take please.");
int steps = scan.nextInt();
int x = 0;
int y = 0;
int XorY;
int dist;
int count =0;
while(count<steps){
XorY = rand.nextInt(2);
dist = rand.nextInt(2);
if(XorY==0){
if(dist==0)
dist = -1;
x += dist;
System.out.println("("+x+", " +y+")");
}
else{
if(dist==0)
dist = -1;
y += dist;
System.out.println("("+x+", " +y+")");
}
count ++;
}
System.out.println("Squared Distance = " + (x*x + y*y));
}
}

第二部分是我遇到问题的地方。它说。

Write a program RandomWalkers.java that takes two integer command-line arguments N and T. In each of T independent experiments, simulate a random walk of N steps and compute the squared distance. Output the mean squared distance (the average of the T squared distances.)

% java RandomWalkers 100 10000
squared distance = 101.446

% java RandomWalkers 100 10000
mean squared distance = 99.1674

% java RandomWalkers 200 1000
mean squared distance = 195.75

我想我可以在外部做一个嵌套的 while 循环,同时计算内部的试验次数,同时与上面完全相同。但它只是打印回第一个试验距离,而不是所有试验的平均值。我已经弄乱它​​好几天了,现在看起来是这样的。感谢任何和所有的帮助。

package project2;

import java.util.*;
import java.math.*;

public class RandomWalkers {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("Enter the number of steps you want to take please.");
int steps = scan.nextInt();
System.out.println("Enter the amount of trials you want run please");
int trials = scan.nextInt();
double avgDist =0;
int stepCount =0;
int trialCount =0;
int x = 0;
int y = 0;
int XorY;
int dist;

while(trialCount<trials){

while(stepCount<steps){
XorY = rand.nextInt(2);
dist = rand.nextInt(2);
if(XorY==0){
if(dist==0)
dist = -1;
x += dist;
}
else{
if(dist==0)
dist = -1;
y += dist;
}
stepCount ++;


}
avgDist += ((x*x) + (y*y));
trialCount++;
}
System.out.println("Average Squared Distancee = " +avgDist/(double)trialCount);
}
}

最佳答案

您应该开始使您的代码更具可读性和可维护性,即使是您自己,也可以将问题分成几个单元,这些单元将成为方法。这是你必须做的:

  1. 进行 T 实验。每个实验都会产生一个距离
  2. 计算距离的平均值

请注意,第一步(询问用户 N 和 T 值)也可能是第一步。但这甚至没有必要,因为赋值要求将它们作为命令行参数传递:它们位于传递给 main() 方法的数组中。

如果您为这两个步骤编写代码,最终会得到以下代码:

double totalDistance = 0;
for (int i = 0; i < t; i++) {
int distance = makeExperiment(n);
totalDistance += distance;
}
double averageDistance = totalDistance / t;

所以现在您只需要实现 makeExperiment() 方法。但这很容易,因为您已经在作业的第一部分中实现了它。您只需删除无用的代码(向用户询问 N 并显示实验结果的部分),并将其放入返回距离的方法中即可。

关于java - 初学者,我遇到了嵌套 while 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com