Java 如何将2个for循环放入for循环中
因此,如果一个女人生了一个男孩,她只能生多个孩子——如果她生了一个女孩,她就不能再生孩子了。我必须创建一个表格,显示运行10个模拟的结果,每个模拟包含在这项政策下10000名母亲所生男孩与女孩的比率。我们假设每个孩子出生后都会有一个孩子(没有双胞胎、三胞胎等),每个孩子都能活下来,生男孩的几率是50% 到目前为止,我已经:Java 如何将2个for循环放入for循环中,java,loops,Java,Loops,因此,如果一个女人生了一个男孩,她只能生多个孩子——如果她生了一个女孩,她就不能再生孩子了。我必须创建一个表格,显示运行10个模拟的结果,每个模拟包含在这项政策下10000名母亲所生男孩与女孩的比率。我们假设每个孩子出生后都会有一个孩子(没有双胞胎、三胞胎等),每个孩子都能活下来,生男孩的几率是50% 到目前为止,我已经: for (numSimulation = 0; numSimulation < 10; numSimulation++){; for (int
for (numSimulation = 0; numSimulation < 10; numSimulation++){;
for (int mothers = 1; mothers <= 10000; mothers++){;
int randomNum = randomGenerator.nextInt();
boolean isMale = randomNum % 2 == 0;
if (isMale){
numMales++;
}
else{
numFemales++;
}}
femaleToMaleRatio =(double) numFemales / numMales;
}
System.out.printf("Run# M : F%n");
System.out.printf("%4d 1 : %.5f%n",numSimulation-9, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-8, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-7, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-6, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-5, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-4, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-3, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-2, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation-1, femaleToMaleRatio);
System.out.printf("%4d 1 : %.5f%n",numSimulation, femaleToMaleRatio);
}}`
for(numSimulation=0;numSimulation<10;numSimulation++){;
对于(int mothers=1;mothers我将执行一段时间(!isFemale){//do randomnume generator,if male然后male++if male然后递增女性变量并将女性布尔值设置为true。下次它尝试进入while循环时,它将停止,因为女性为true。}这个while循环应该在另外两个for循环之后。希望这有帮助。我会做一段时间(!isFemale){//do randomnume generator,if male然后male++if female然后增加女性变量并将女性布尔值设置为true。下次它尝试进入while循环时,它会停止,因为女性为true。}这个while循环应该在另外两个for循环之后。希望这有帮助。试试这个,希望这有帮助
for (numSimulation = 0; numSimulation < 10; numSimulation++){
boolean isfemale=false;
for (int mothers = 1; mothers <= 10000; mothers++){
int randomNum = randomGenerator.nextInt();
do{
boolean isMale = randomNum % 2 == 0;
if (isMale){
numMales++;
}
else{
isfemale=true;
numFemales++;
}
}while(!isfemale);
}
femaleToMaleRatio =(double) numFemales / numMales;
}
for(numSimulation=0;numSimulation<10;numSimulation++){
布尔值isfemale=false;
对于(int mothers=1;mothers试试这个希望这有帮助
for (numSimulation = 0; numSimulation < 10; numSimulation++){
boolean isfemale=false;
for (int mothers = 1; mothers <= 10000; mothers++){
int randomNum = randomGenerator.nextInt();
do{
boolean isMale = randomNum % 2 == 0;
if (isMale){
numMales++;
}
else{
isfemale=true;
numFemales++;
}
}while(!isfemale);
}
femaleToMaleRatio =(double) numFemales / numMales;
}
for(numSimulation=0;numSimulation<10;numSimulation++){
布尔值isfemale=false;
对于(int mothers=1;mothers希望它能帮助你们所有人
public static void main(String[] args) throws IOException {
int numMales = 0;
int numFemales = 0;
int numSimulation = 0;
double femaleToMaleRatio = 0;
System.out.printf("Run# M : F%n");
for (numSimulation = 0; numSimulation < 10; numSimulation++) {
for (int mothers = 1; mothers <= 10000; mothers++) {
int randomNum = randInt(0,1);
boolean isMale = randomNum % 2 == 0;
boolean isFemale = false;
while(!isFemale){
isFemale = randInt(0,1) == 0;
}
if (isMale) {
numMales++;
} else {
numFemales++;
}
}
femaleToMaleRatio = (double) numFemales / numMales;
System.out.printf("%4d 1 : %.15f%n", numSimulation, femaleToMaleRatio);
}
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
publicstaticvoidmain(字符串[]args)引发IOException{
整数=0;
int numFemales=0;
整数模拟=0;
双股骨距=0;
System.out.printf(“Run#M:F%n”);
对于(numSimulation=0;numSimulation<10;numSimulation++){
对于(int mothers=1;mothers希望它能帮助你们所有人
public static void main(String[] args) throws IOException {
int numMales = 0;
int numFemales = 0;
int numSimulation = 0;
double femaleToMaleRatio = 0;
System.out.printf("Run# M : F%n");
for (numSimulation = 0; numSimulation < 10; numSimulation++) {
for (int mothers = 1; mothers <= 10000; mothers++) {
int randomNum = randInt(0,1);
boolean isMale = randomNum % 2 == 0;
boolean isFemale = false;
while(!isFemale){
isFemale = randInt(0,1) == 0;
}
if (isMale) {
numMales++;
} else {
numFemales++;
}
}
femaleToMaleRatio = (double) numFemales / numMales;
System.out.printf("%4d 1 : %.15f%n", numSimulation, femaleToMaleRatio);
}
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
publicstaticvoidmain(字符串[]args)引发IOException{
整数=0;
int numFemales=0;
整数模拟=0;
双股骨距=0;
System.out.printf(“Run#M:F%n”);
对于(numSimulation=0;numSimulation<10;numSimulation++){
对于(int mothers=1;mothers好的,所以前两个循环是正确的,除了后面的分号,正如其他人指出的那样
现在开始分娩。母亲会分娩。如果她生了男孩,她会继续分娩。如果她生了女孩,她会停止。对吗?这可以反映在一个while循环中:
while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
numMales++; // increment the boys counter
}
numFemales++; // we've stopped giving birth to boys -> we've given birth to a girl and should stop
现在显示每个模拟的雌雄比。Simpy输出每次模拟后的雌雄比为don,即在第一个for循环的末尾
完整的代码可能如下所示
for (int numSimulation = 0; numSimulation < 10; numSimulation++) {
// reset the boys and girls counter before each simulation
int numMales = 0;
int numFemales = 0;
double femaleToMaleRatio = 1;
for (int mothers = 1; mothers <= 10000; mothers++) { // here the simulation starts
while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
numMales++; // increment the boys counter
}
// we've stopped giving birth to boys -> we've given birth to a girl and should stop
numFemales++;
}
// we only need to calculate the female to male ratio after the simulation is done
femaleToMaleRatio = (double) numFemales / numMales;
// then we print the result of our simulation once its done
System.out.printf("%4d 1 : %.5f%n", numSimulation, femaleToMaleRatio);
}
for(int numSimulation=0;numSimulation<10;numSimulation++){
//在每次模拟之前重置男孩和女孩计数器
整数=0;
int numFemales=0;
双女性耐力=1;
对于(int mothers=1;母亲们,我们已经生了一个女孩,应该停止
numFemales++;
}
//我们只需要在模拟完成后计算男女比例
女性耐力=(双)numFemales/numMales;
//一旦模拟完成,我们就打印模拟结果
System.out.printf(“%4d 1:%.5f%n”,数值模拟,女性宽容);
}
希望这能有所帮助。好的,所以前两个循环是正确的,除了后面的分号,正如其他人指出的那样
现在开始分娩。母亲会分娩。如果她生了男孩,她会继续分娩。如果她生了女孩,她会停止。对吗?这可以反映在一个while循环中:
while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
numMales++; // increment the boys counter
}
numFemales++; // we've stopped giving birth to boys -> we've given birth to a girl and should stop
现在显示每个模拟的雌雄比。Simpy输出每次模拟后的雌雄比为don,即在第一个for循环的末尾
完整的代码可能如下所示
for (int numSimulation = 0; numSimulation < 10; numSimulation++) {
// reset the boys and girls counter before each simulation
int numMales = 0;
int numFemales = 0;
double femaleToMaleRatio = 1;
for (int mothers = 1; mothers <= 10000; mothers++) { // here the simulation starts
while(randomGenerator.nextInt() % 2 == 0) { // while we're giving birth to boys
numMales++; // increment the boys counter
}
// we've stopped giving birth to boys -> we've given birth to a girl and should stop
numFemales++;
}
// we only need to calculate the female to male ratio after the simulation is done
femaleToMaleRatio = (double) numFemales / numMales;
// then we print the result of our simulation once its done
System.out.printf("%4d 1 : %.5f%n", numSimulation, femaleToMaleRatio);
}
for(int numSimulation=0;numSimulation<10;numSimulation++){
//在每次模拟之前重置男孩和女孩计数器
整数=0;
int numFemales=0;
双女性耐力=1;
对于(int mothers=1;母亲们,我们已经生了一个女孩,应该停止
numFemales++;
}
//我们只需要在模拟完成后计算男女比例
女性耐力=(双)numFemales/numMales;
//一旦模拟完成,我们就打印模拟结果
System.out.printf(“%4d 1:%.5f%n”,数值模拟,女性宽容);
}
希望这能有所帮助。以Ashwin Shenoy的话为基础:
为了做到这一点,你基本上必须在生了男孩而不是女孩之后进行递归调用(或while语句)
这看起来像:
public static double getFemaleToMaleRatio() {
// total number of runs
int numberOfRuns = 0;
// total number of females
int numFemales = 0;
// total number of males
int numMales = 0;
// run n number of times
while(numberOfRuns < 10) {
int mothers = 0;
while(mothers < 10000) {
// keep running here until "makeBabyGirl()" returns true
while(!makeBabyGirl()) {
numMales++;
}
// once "makeBabyGirl()" returns true, it breaks the while loop and increments "numFemales"
numFemales++;
mothers++;
}
numberOfRuns++;
System.out.printf("Ratio: %s\n", (double) numFemales/ numMales);
}
System.out.printf("Final Ratio: %s\n", (double) numFemales/ numMales);
return (double) numFemales/ numMales;
}
public static boolean makeBabyGirl() {
// 50% change of having a boy or girl
boolean isFemale = Math.random() < 0.5;
return isFemale;
}
公共静态双GetFemaleTomalertio(){
//总运行次数
int numberOfRuns=0;
//女性总数
int numFemales=0;
//男性总人数
整数=0;
//运行n次
同时(运行次数<10){
int=0;
而(母亲<10000){
//继续在此处运行,直到“makeBabyGirl()”返回true
而(!makeBabyGirl()){
numMales++;
}
//一旦“makeBabyGirl()”返回true,它就会中断while循环并递增“numFemales”
numFemales++;
母亲++;
}
numberOfRuns++;
System.out.printf(“比率:%s\n”,(双)nummemales/numMales);
}
System.out.printf(“最终比率:%s\n”,(双)nummemales/numMales);
返回(双)数值/数值;
}
公共静态布尔makeBabyGirl(){
//生男孩或女孩的50%变化
布尔值isFemale=Math.random()<0.5;
回报是女性;
}
你可以把你的2英镑留给我