2

我在使用一个人从 7 英尺桥中间开始的程序时遇到问题。这是描述:

有人站在一座 7 英尺长的桥的中心。他们的步幅正好是一英尺。他们无法控制自己前进的方向,但桥很窄,每一步只能前进或后退。

编写一个程序,计算人在离开桥之前要走多少步。让程序执行此模拟 1000 次,并在输出中显示所采取的平均步数和最大步数。(提示:生成一个 0 或 1 的随机数,让一个向前相等,另一个向后相等)。这样做 20 次,以便您可以进行比较观察。

这是我到目前为止所拥有的:

import java.util.Random;
import java.util.Scanner;
import java.text.DecimalFormat;

public class prog214a
{
    public static void main(String[] args)
    {
        Random generator = new Random();
        System.out.println("1000 iterations");
        int runs = 0;
        int iter = 1000;
        double count = 7.0 / 2.0;
        int random;
        System.out.println("Run\tAvarage\tGreatest Number of Steps");
        // for(runs=1;runs<20; runs+=1)
        // {
        for (iter = 1000; iter > 1; iter -= 1)
        {
            double tries = 1;
            double avg = count / tries;
            random = generator.nextInt(2);
            if (random == 0)
            {
                count -= 1;
            }
            if (random == 1)
            {
                count += 1;
            }
            if (count <= 0 || count >= 7)
            {
                System.out.println("#" + runs + ":\t" + avg + "\t" + count);
                count = 0;
                runs += 1;
            }
            tries += 1;
        }
        // }
    }
}
4

1 回答 1

3

Part of your problem is the way you're approaching it. Your code should show a neat path from the abstract set of things you're doing to an actual mechanical execution.

Your first problem is establishing an algorithm, or sequence of events you want to happen:

Print out position
Decide direction
Move
Repeat n times

Now lets translate those gross steps into code. We do this by making methods for each:

public void printPosition();
public int findDirectionVector();
public void updatePosition(int move);//move is a vector
public void runSimulation(int iterations);

Lets now fill out the control method:

public void runSimulation(int iterations) {
   for (n = 0; n < iterations ; n++) {
       executeIteration();//simple! 
   }
}

private void executeIteration() {
  printPosition();
  updatePosition(findDirectionVector());
}

As you can see, we're making sure each method does exactly one thing. We are also leaving certain things (such as Position) as a member variable of the class rather than trying to handle it as a variable you're passing around. However, this is a change, so lets look at your initialization and class structure:

public Simulation {
  private int position = 0;
  private int iterations = 0;//number of times the person moves before you stop!

  public Simulation(int iterations) {
    this.iterations = iterations;
  }

  public static void main(String[] args) {
    Simulation sim = new Simulation(1000);//Run with 1000 iterations
    sim.runSimulation();
  }

  //your other methods go here
}

Now, when you want to run this 1000 times you just have to wrap sim.runSimulation() in a for loop.

I've left out one very important check (has the man exited the bridge?) and the implementations of the other methods for you to do.

于 2012-11-07T01:57:50.300 回答