6

我正在处理一个我有点困惑的问题。问题是假设您是二战期间英国空军的将军。你还有 100 架飞机可以保卫英国。每执行一次任务,每架飞机都有 50% 的几率被德国高射炮击落,因此每次执行任务都会损失大约一半的飞机。你必须编写一个程序来估算每次任务后有多少飞机可以存活,以及在所有飞机被击落之前你可以执行多少次任务。

我的程序不起作用,我不知道它有什么问题,所以我猜英格兰有麻烦了。我试图用两个while循环来解决这个问题。外部 while 循环表示,只要您还有飞机,就可以将它们发送到另一个任务中。内部 while 循环模拟实际任务。在 while 循环存在之后,平面的总数现在是幸存的平面。

import acm.program.*; 
import acm.util.*;

public class MissionPlanes extends ConsoleProgram{
public void run(){

  int planes = 100;  /* total number of planes */
  int suvPlanes = 0;  /* surviving planes  */
  int mission = 0;      /* total number of missions */
  int planeCounter = 0;   /* keeps track of the planes flying over the anti plane gun  */


  while (planes > 0){

       while(planeCounter < planes){
             planeCounter++;
             if(rgen.nextBoolean()){   /* I've tried rgen.nextBoolean() with paramaters and with no paramaters */
              suvPlanes += 1;
                   }
            }
    planes = suvPlanes;
    mission++;
 println("The total number of surviving planes you have is " + planes + "after" + missoin + "missions"); 
     }
  }
  private RandomGenerator rgen = RandomGenerator.getInstance();
      }
4

5 回答 5

8

您必须planeCounter在外循环中重置为 0。同样适用于suvPlanes

while (planes > 0){
  planeCounter = 0;
  suvPlanes = 0;
  // ... remaining stuff

如果您在此循环的第二次迭代中不这样做,您最终planeCounter >= planes将不会执行内部循环。另一方面suvPlanes,不会重置为 0,因此平面将永远保持等于suvPlanes第一个循环中的值,因此您的外循环将永远不会终止。

于 2013-06-27T06:55:42.997 回答
3

您应该重置 planeCounter 和 survivingPlanes。

于 2013-06-27T07:01:14.793 回答
2

您的课程没有 main 方法(我假设您自己运行它)。代码中还有一些逻辑错误,以及一些导入语句,至少我的编译器对此并不满意。

我已经清理它并添加了一个主要方法:

import java.util.Random;

public class MissionPlanes {


    public static void main(String[] args){
        Random rgen = new Random();

        int planes = 100;  /* total number of planes */
        int suvPlanes = 0;  /* surviving planes  */
        int mission = 0;      /* total number of missions */
        int planeCounter = 0;   /* keeps track of the planes flying over the anti plane gun  */


        while (planes > 0){

            while(planeCounter < planes){
                planeCounter++;
                if(rgen.nextBoolean()){   /* I've tried rgen.nextBoolean() with paramaters and with no paramaters */
                suvPlanes ++;
                }
            }
            planes = suvPlanes;
            suvPlanes = 0;
            planeCounter = 0;
            mission++;
            System.out.println("The total number of surviving planes you have is " 
            + planes + " after " + mission + " missions"); 
        }
  }
}
于 2013-06-27T07:07:01.503 回答
1

我重新修改了你的代码,你可以运行这个`

static Random mRandom = new Random();
static int totalPlanes = 100;

public static void main(String[] args) {
    run();
}

public static void run() {
    int planes = totalPlanes; /* total number of planes */
    int suvPlanes = 0; /* surviving planes */
    int mission = 0; /* total number of missions */
    int planeCounter = 0; /*
                         * keeps track of the planes flying over the anti
                         * plane gun
                         */

    // it is default that it would encounter the anti plane gun if its in a
    // mission so don't use planeCounter
    // and this method assume that the general is sending one plane at a
    // time
    while (planes > 0) {
        if (mRandom.nextBoolean()) {// 50% chance it can survive
            suvPlanes += 1;
        } else {
            // decrease the plane count when is not survived
            planes -= 1;
        }
        mission++;
        System.out
                .println("The total number of survived planes you have is "
                        + suvPlanes + " after " + mission
                        + " missions and " + "the original no of plane "
                        + planes);
    }
}

` 快跑来得到答案

于 2013-06-27T07:19:20.970 回答
1

您的程序无法编译。

http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextBoolean()说 nextBoolean() 没有参数,但你给它一个。可能您的程序没有编译并且您正在运行它的旧版本。

于 2013-06-27T06:50:58.640 回答