难以理解计数和平均值

时间:2014-11-10 03:29:19

标签: java if-statement count computer-science

描述:我必须创建一个模拟站在七英尺桥上的男人的程序,而男人站在中间(3.5英尺)。我必须使用随机数模拟器并将0或1. 1表示向前一步,而0表示向后一步。当男子走出桥(小于0或大于7)时,它会重新启动。我应该运行1000次(输出中未显示)。然后,我必须计算他走之前所采取的平均步数。另外,我必须计算他在1000次跑步中所采取的最大步数。到目前为止,我在创建计数,制作平均值以及如何计算最大步数方面遇到了麻烦。任何人都可以帮助我理解一个更好的方法来查看我的编码来完成所有这些。任何帮助将不胜感激。

import java.util.*;
public class Prog214a
{
   public static void main(String[] args)
   {
       int greatesNumOfSteps;
       double bridge,sum,avg;
       int beginning = 1;
       double midway = 3.5;

       System.out.println("Run     Average     Greatest Number of Steps");

       for(beginning = 1; beginning <= 1000; beginning++)
       {
           Random randomNum = new Random();
           int step = randomNum.nextInt(2);
           if(step == 1)
           {
               midway = midway + 1;
               sum = midway;

           }
           else
           {
               midway = midway - 1;
               sum = midway;
           }
           if(midway < 0.5 || midway > 6.5)
           {
              System.out.println("#" + beginning + "     ...");
              midway = 3.5;
           }
       }
   }
}

/**My Output:

Run     Average     Greatest Number of Steps
#10     ...
#34     ...
#46     ...
#58     ...
#122     ...
#132     ...
#148     ...
#160     ...
#188     ...
#218     ...
#228     ...
#266     ...
#312     ...
#322     ...
#338     ...
#344     ...
#376     ...
#394     ...
#398     ...
#408     ...
#418     ...
#426     ...
#438     ...
#444     ...
#452     ...
#470     ...
#480     ...
#526     ...
#542     ...
#546     ...
#562     ...
#576     ...
#584     ...
#616     ...
#662     ...
#668     ...
#704     ...
#726     ...
#732     ...
#740     ...
#750     ...
#792     ...
#798     ...
#804     ...
#814     ...
#818     ...
#830     ...
#840     ...
#844     ...
#850     ...
#864     ...
#874     ...
#884     ...
#900     ...
#914     ...
#918     ...
#928     ...
#968     ...
#978     ...
*/

这应该是这样的:

运行平均最大步数

1 xx.xx xx

2 个答案:

答案 0 :(得分:1)

你应该把它归结为许多不同的子问题:

  1. 您需要运行1000次测试
  2. 对于每次执行,您需要模拟人员离开桥梁所需的步数
  3. 虽然这名男子不在桥下,但您需要随机查看下一步的方向并接受它
  4. 跟踪他总共采取了多少步骤
  5. 当他离开桥梁时,比较此执行所采取的步骤数量与所有其他执行相比
  6. 跟踪所有执行中的总计步数,以便计算平均值
  7. 从描述中可能看起来不需要为每次运行输出任何内容,只有在每次执行后才能输出。这是一个基于你的文件的小骨架(我希望我不会放弃太多),这应该会告诉你一个更好的结构。

    import java.util.*;
    
    public class Prog214a {
        // Bridge is 7 feet
        private static final float BRIDGE_LENGTH = 7.0f;
        // Man starts at 3.5 ft
        private static final float MAN_START = 3.5f;
        // [1] Times to run the test
        private static final int TOTAL_ITERATIONS = 1000;
    
        public static void main(String[] args) {
    
            // [5] Initialize a max low so that first iteration will always be greater
            int greatestNumOfSteps = Integer.MIN_VALUE;
            // [6] Initialize the total number of steps over all executions
            int totalSteps = 0;
    
            // [2] Generate a random number for this program execution
            final Random randomNum = new Random();
    
            System.out.println("Average\tGreatest Number of Steps");
    
            for (int iteration = 1; iteration <= TOTAL_ITERATIONS; iteration++) {
                // [2] Initialize this current man's starting position
                float currentManPosition = MAN_START;
                // [4] Initialize the amount of steps the man has taken this time
                int stepsTaken = 0;
                while (// [3] condition to check both sides of the bridge) {
                    // [3] Use a boolean to determine if he should step forward or not since those are the only 2 possibilities (can use int if needed)
                    boolean shouldStepForward = randomNum.nextBoolean();
                    if (shouldStepForward) {
                        currentManPosition = currentManPosition + 1;
                    } else {
                        currentManPosition = currentManPosition - 1;
                    }
                    // [4] what would do to keep track of the steps for this iteration?
                }
                if (// [5] Check if this runs steps were bigger than the greatest so far) {
                }
                // [6] Add steps to the total
                totalSteps += stepsTaken;
            }
            // [6] Compute the average of the total steps
            System.out.printf("%.2f\t%d\n", averageSteps, greatestNumOfSteps);
        }
    }
    

    当我跑步时,平均步数通常在16左右,最大步数通常在80-90范围内,但变化很大。

答案 1 :(得分:1)

组织计算机程序的一种方法是使用Petri的网络元素。根据这些网络元素组织计算机程序也称为Petri网。

以下是从我创建的PDF文件派生的图像。 PDF version包含一个运行交互式模拟的JavaScript程序。

enter image description here enter image description here