Java RandomGenerator-损失50%的飞机模拟
我正在处理一个我有点困惑的问题。问题是假设你是二战期间英国空军的将军。你还有100架飞机去保卫英国。每执行一次任务,每架飞机都有50%的几率被德国高射炮击落,因此每执行一次任务,你都会损失大约一半的飞机。您必须编写一个程序,该程序将估算每次任务后幸存的飞机数量,以及在所有飞机被击落之前可以执行的任务数量Java RandomGenerator-损失50%的飞机模拟,java,Java,我正在处理一个我有点困惑的问题。问题是假设你是二战期间英国空军的将军。你还有100架飞机去保卫英国。每执行一次任务,每架飞机都有50%的几率被德国高射炮击落,因此每执行一次任务,你都会损失大约一半的飞机。您必须编写一个程序,该程序将估算每次任务后幸存的飞机数量,以及在所有飞机被击落之前可以执行的任务数量 我的程序不起作用,我不知道有什么问题,所以我想英格兰有麻烦了。我试图用两个while循环来解决这个问题。外部的while循环表示,只要你还有飞机,就让他们执行另一项任务。内部while循环模拟实
我的程序不起作用,我不知道有什么问题,所以我想英格兰有麻烦了。我试图用两个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();
}
导入acm.program.*;
导入acm.util.*;
公共级任务飞机扩展控制台程序{
公开募捐{
int planes=100;/*平面总数*/
int suvPlanes=0;/*幸存平面*/
int任务=0;/*任务总数*/
int planeCounter=0;/*跟踪在反飞机炮上方飞行的飞机*/
而(平面>0){
while(平面计数器<平面){
平面计数器++;
if(rgen.nextBoolean()){/*我尝试过rgen.nextBoolean()带有参数,但没有参数*/
suvPlanes+=1;
}
}
飞机=越野车;
任务++;
println(“在“+missoin+”任务之后,您的幸存飞机总数为“+planes+”);
}
}
private RandomGenerator rgen=RandomGenerator.getInstance();
}
您的程序无法编译
表示nextBoolean()不接受任何参数,但您正在为其提供一个参数。可能您的程序没有编译,您正在运行较旧版本的程序。您必须在外部循环中将
planeCounter
重置为0。同样适用于suvPlanes
:
while (planes > 0){
planeCounter = 0;
suvPlanes = 0;
// ... remaining stuff
如果在此循环的第二次迭代中不执行此操作,则将以
planeCounter>=planes
结束,因此不会执行内部循环。另一方面,suvPlanes
将不会重置为0,因此在第一个循环中,平面将永远保持等于suvPlanes
的值,因此您的外部循环将永远不会终止。您应该重置planeCounter和survivingPlanes。您的类没有主方法(我假设您自己在运行它)。代码中还有一些逻辑错误,以及一些导入语句,至少我的编译器不满意
我已经清理了它并添加了一个主要方法:
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");
}
}
}
import java.util.Random;
公共级任务飞机{
公共静态void main(字符串[]args){
Random rgen=新的Random();
int planes=100;/*平面总数*/
int suvPlanes=0;/*幸存平面*/
int任务=0;/*任务总数*/
int planeCounter=0;/*跟踪在反飞机炮上方飞行的飞机*/
而(平面>0){
while(平面计数器<平面){
平面计数器++;
if(rgen.nextBoolean()){/*我尝试过rgen.nextBoolean()带有参数,但没有参数*/
suvPlanes++;
}
}
飞机=越野车;
suvPlanes=0;
平面计数器=0;
任务++;
System.out.println(“您拥有的幸存飞机总数为”
+飞机+“在”+任务+“任务”之后);
}
}
}
我重新修改了您的代码,您可以运行此`
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);
}
}
`只需运行获取答案请详细说明“它不工作”。它以什么方式不工作?它是否接受任何参数?@Patashu什么都没有发生。它不会在屏幕上打印任何内容。我相信我做的一切都是正确的。抽象!如果内部循环计算SUVPLANE(生存平面),它不应该在循环之前以0开头吗?@JessicaM.Nothing?您确定您看到了控制台吗?并且您确实调用了这个
run()
某个地方的方法?我在参数为.5和没有参数的情况下都尝试过。仍然不起作用。我不知道哪里出了问题。@JessicaM。你需要阅读程序的编译器错误并解决它们。现在就尝试你的建议,看看我是否能让它起作用。我已经尝试了你的建议。它有效,并且说“完成6次任务后,你的幸存飞机总数为0"我正在尝试打印第一次任务后的飞机数量,然后是第二次任务后的飞机数量,然后是第三次任务,直到我没有飞机了。它工作了…它终于工作了!!!!非常感谢你的帮助。所以你必须在每个循环开始之前重置planeCounter和suvPlanes,因为它们仍然具有上一个循环的值。对吗?Jessica,一个po可能的解决方案是使用for(int planeCounter=0;planeCounter=