RandomGenerator - 50% loss of airplane simulation

I am working on a problem that I'm a bit confused about. The question is that you are a British Air Force general during World War II. You have 100 planes left to defend the United Kingdom. With each mission, you fly on each plane, having a 50% chance of shooting down German anti-tank guns so that each mission will lose about half of your planes. You should write a program that will approximate how many planes will survive after each mission and how many missions you can launch until all your planes are shot down.

My program does not work, and I don’t know what is wrong with it, so I think England has problems. I am trying to solve this problem with two while loops. The outer while loop says that if you have planes, send them to another mission. The inner while loop simulates a real mission. After the while loop exists, the total number of planes is now surviving.

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(); } 
+6
source share
5 answers

In the outer loop, you will need to reset planeCounter to 0. The same is true for suvPlanes :

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

If you do not do this in the second iteration of this loop, you will get planeCounter >= planes so that you do not execute the inner loop. On the other hand, suvPlanes will not reset to 0, so the planes will always be equal to the value of suvPlanes in the first cycle, and therefore your outer cycle will never end.

+8
source

You must reset both planeCounter and survivePlanes.

+3
source

Your class does not have a main method (I assume that you use it on it). There are also some logical errors in the code, as well as some import statements, that my compiler is at least unhappy with.

I cleaned it up and added the main method:

 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"); } } } 
+2
source

Your program does not compile.

http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextBoolean () says nextBoolean () does not accept parameters, but you give it. Your program is probably not compiling, and you are using its older version.

+1
source

I changed the code and you can run this `

 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); } } 

`Just run to get an answer

+1
source

Source: https://habr.com/ru/post/948193/


All Articles