Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解计数和平均数有困难_Java_If Statement_Count_Computer Science - Fatal编程技术网

Java 理解计数和平均数有困难

Java 理解计数和平均数有困难,java,if-statement,count,computer-science,Java,If Statement,Count,Computer Science,描述:我必须创建一个程序,模拟一个人站在七英尺高的桥上,而这个人站在中间(3.5英尺)。我必须使用一个随机数模拟器,设置为0或1。1表示前进一步,0表示后退一步。当该男子从驾驶台上走下(小于0或大于7)后,驾驶台重新启动。我应该运行1000次(输出中未显示)。然后,我必须计算出他离开前平均走多少步。此外,我还要计算他在1000次跑步中所走的最大步数。到目前为止,我在创建计数、进行平均以及如何计算最大步数方面遇到了困难。有谁能帮助我更好地了解我的代码来完成这一切。任何帮助都将不胜感激 import

描述:我必须创建一个程序,模拟一个人站在七英尺高的桥上,而这个人站在中间(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     ...
*/
import java.util.*;
公共类Prog214a
{
公共静态void main(字符串[]args)
{
int greatesnoof步骤;
双桥,总和,平均值;
int开始=1;
双中段=3.5;
System.out.println(“运行平均最大步数”);
用于(开始=1;开始6.5)
{
System.out.println(“#”+开头+”);
中途岛=3.5;
}
}
}
}
/**我的输出:
运行平均最大步数
#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您应该将其归结为许多不同的子问题:

  • 您需要运行1000次测试
  • 对于每一次执行,您都需要运行模拟,以了解该人从桥上下来需要多少步
  • 当这个人还没有离开桥时,你需要随机看看他下一步会朝哪个方向走,然后采取行动
  • 记录他总共走了多少步
  • 当他走下驾驶台时,将此执行与所有其他执行进行比较
  • 跟踪所有执行中总共执行了多少步,以便计算平均值
  • 根据描述,听起来您不需要为每次运行输出任何内容,只需要在每次执行之后。这里是一个基于您的文件的小框架(我希望我没有透露太多),它应该会向您展示一个更好的结构

    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);
        }
    }
    
    import java.util.*;
    公共类Prog214a{
    //桥高7英尺
    专用静态最终浮桥长度=7.0f;
    //男子从3.5英尺开始
    专用静态最终浮子手动启动=3.5f;
    //[1]次运行测试
    私有静态最终整数总迭代次数=1000;
    公共静态void main(字符串[]args){
    //[5]初始化最大下限,以便第一次迭代总是更大
    int greatestNumOfSteps=Integer.MIN_值;
    //[6]初始化所有执行的步骤总数
    int-totalSteps=0;
    //[2]为此程序执行生成一个随机数
    最终随机数=新随机数();
    System.out.println(“平均\t最大步数”);
    
    对于(int iteration=1;iteration组织计算机程序的一种方法是使用Petri网元素。根据这些网络元素组织计算机程序也称为Petri网

    下面的图片来自我创建的一个PDF文件,包括一个运行交互式模拟的JavaScript程序