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