不知道为什么这不起作用 [英] Not sure why this is not working

查看:65
本文介绍了不知道为什么这不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我坚持上学的程序.无法弄清楚为什么它不能给我带来平均和最大的收益.该程序可以像我希望的那样运行50次,但不会为移动和总计添加值.

This is a program i'm stuck on for school. Can't figure out why it won't give me a average and a greatest. The program runs 50 times like i want it to but does not add a value to moves and total.

import java.util.*;

public class RandomWalk {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int location;
        double average;
        int greatest=0;
        int moves = 0;
        int total = 0;
        int step;

        for (int i = 0; i < 50; i++) {

            location = 4;
            step = rand.nextInt((2 - 1) + 1) + 1;


            while (location < 1 || location > 7) {
                moves ++;
                if (step == 2){
                    location ++;    
                } else {
                    location --;
                }
                if (moves > greatest) {
                    greatest = moves;
                    total += moves;
                }
            }
        }

        average = total / 50;

        System.out.println("The greatest number of steps: " + greatest);
        System.out.println("The average number of steps:  " + average);

     }

}

修复了for循环,但仍然无法获得平均值和最大值.

fixed the for loop but it still does not give me the average and the greatest.

关于while (location < 1 || location > 7),它应该一直运行到该人站在位置1或位置7为止.

as for the while (location < 1 || location > 7) it should run till the person stands on location 1 or location 7.

这是我遇到的问题:

在随机行走"中,一个人被放置在一座七米长的桥的中央.每一步都会使一个人随机向前或向后移动1米. 创建一个随机行走,以确定人在离开桥上之前要走多少步.对该应用程序进行平均50次试用,并显示平均步骤数和最大步骤数.

In the "random walk", a person is placed at the center of a seven meter long bridge. Each step moves a person 1 meter either forward or backward at random. Create a randomwalk that determines how many steps the person will walk before taking a step off the bridge. Have the application average 50 trials, and display the average and the greatest number of steps.

感谢您的帮助...

如果我将其更改为:`import java.util.*;

if i change it to this: `import java.util.*;

公共类RandomWalk2 {

public class RandomWalk2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Random rand = new Random();
    double average;
    int greatest=0;
    int moves;
    int total = 0;
    int step;

    step = rand.nextInt((2 - 1) + 1) + 1;

    int location; //location of man on bridge
    for(int i = 0; i < 50; i++)
    {
       moves = 0;
       moves++;
       location = 4;
       total += moves;
       while(true)
       {
          if (step == 2) {
           location++;
          } else {
           location--;
          }

         if((location < 1) || (location > 7)) break;
       }

      if(moves > greatest) {
      greatest = moves;

      }


      }

    average = total / 50;

    System.out.println("The greatest number of steps: " + greatest);
    System.out.println("The average number of steps:  " + average);


    }

}

` 然后它运行了50次,但该家伙只移动了1个停靠点,所以我的最大和平均值始终显示为1.

` then it runs 50 times but the guy only moves 1 stop so my greatest and average always shows as 1.

推荐答案

不,它不会循环50次,因为您的for循环条件不正确.如果条件是true而不是false,则for循环将继续迭代.更改您的条件

No, it doesn't loop 50 times, because your for loop condition is incorrect. The for loop continues iterating if the condition is true, not false. Change your condition from

for (int i = 0; i == 50; i++) {

for (int i = 0; i < 50; i++) {

然后它将一直运行,直到i < 50false(50次).

Then it will run until i < 50 is false (50 times).

这篇关于不知道为什么这不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆