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; int suvPlanes = 0; int mission = 0; int planeCounter = 0; while (planes > 0){ while(planeCounter < planes){ planeCounter++; if(rgen.nextBoolean()){ suvPlanes += 1; } } planes = suvPlanes; mission++; println("The total number of surviving planes you have is " + planes + "after" + missoin + "missions"); } } private RandomGenerator rgen = RandomGenerator.getInstance(); }
source share