gpt4 book ai didi

java - (Java) 使用嵌套循环

转载 作者:行者123 更新时间:2023-12-01 05:04:58 25 4
gpt4 key购买 nike

我的assignment instructions “掷”2 个骰子并得到总和,然后根据用户想要掷骰子的次数找出总和出现的概率。我必须使用嵌套循环,并且不能为每个骰子组合使用单独的循环(我还没有这样做)。我不允许在此作业中使用数组。

Write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled.

  1. Create a new project called 5.05 Random Dice in the Mod05 Assignments folder.
  2. Create a class called DiceProbability in the newly created project folder.
  3. Ask the user to input how many times the dice will be rolled.
  4. Calculate the probability of each combination of dice. (You may want to start with more familiar six-sided dice.)
  5. Print the results neatly in two columns

我需要帮助来找出我在第二个“for”循环中放入的内容。抱歉,整数和 if 语句的列表很困惑。代码未完成。

import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();

int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11= 0;
int count12= 0;
int count13 = 0;
int count14 = 0;
int count15 = 0;
int count16 = 0;
int count17 = 0;
int count18 = 0;
int count19 = 0;
int count20 = 0;
int count21 = 0;
int count22 = 0;
int die1 = 0, die2 = 0;
int rolls = 0;
int actualDiceSum;
double probabilityOfDice = 0.0;

System.out.print("Number of Rolls: ");
rolls = in.nextInt();

for(int timesRolled = 0; timesRolled < rolls; timesRolled++)
{
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;
for()
{
if(actualDiceSum == 2){
count2++;
probabilityOfDice = count2 / rolls;
}
else if(actualDiceSum == 3){
count3++;
probabilityOfDice = count3 / rolls;
}
else if(actualDiceSum == 4){
count4++;
probabilityOfDice = count4 / rolls;
}
else if(actualDiceSum == 5){
count5++;
probabilityOfDice = count5 / rolls;
}
else if(actualDiceSum == 6){
count6++;
probabilityOfDice = count6 / rolls;
}
else if(actualDiceSum == 7){
count7++;
probabilityOfDice = count7 / rolls;
}
else if(actualDiceSum == 8){
count8++;
probabilityOfDice = count8 / rolls;
}
else if(actualDiceSum == 9){
count9++;
probabilityOfDice = count9 / rolls;
}
else if(actualDiceSum == 10){
count10++;
probabilityOfDice = count10 / rolls;
}
else if(actualDiceSum == 11){
count11++;
probabilityOfDice = count11 / rolls;
}
else if(actualDiceSum == 12){
count12++;
probabilityOfDice = count12 / rolls;
}
else if(actualDiceSum == 13){
count13++;
probabilityOfDice = count13 / rolls;
}
else if(actualDiceSum == 14){
count14++;
probabilityOfDice = count14 / rolls;
}
else if(actualDiceSum == 15){
count15++;
probabilityOfDice = count15 / rolls;
}
else if(actualDiceSum == 16){
count16++;
probabilityOfDice = count16 / rolls;
}
else if(actualDiceSum == 17){
count17++;
probabilityOfDice = count17 / rolls;
}
else if(actualDiceSum == 18){
count18++;
probabilityOfDice = count18 / rolls;
}
else if(actualDiceSum == 19){
count19++;
probabilityOfDice = count19 / rolls;
}
else if(actualDiceSum == 20){
count20++;
probabilityOfDice = count20 / rolls;
}
else if(actualDiceSum == 21){
count21++;
probabilityOfDice = count21 / rolls;
}
else if(actualDiceSum == 22){
count22++;
probabilityOfDice = count22 / rolls;
}
}
}


System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + probabilityOfDice + "%");
System.out.println("3's\t\t" + probabilityOfDice + "%");
System.out.println("4's\t\t" + probabilityOfDice + "%");
System.out.println("5's\t\t" + probabilityOfDice + "%");
System.out.println("6's\t\t" + probabilityOfDice + "%");
System.out.println("7's\t\t" + probabilityOfDice + "%");
System.out.println("8's\t\t" + probabilityOfDice + "%");
System.out.println("9's\t\t" + probabilityOfDice + "%");
System.out.println("10's\t\t" + probabilityOfDice + "%");
System.out.println("11's\t\t" + probabilityOfDice + "%");
System.out.println("12's\t\t" + probabilityOfDice + "%");
System.out.println("13's\t\t" + probabilityOfDice + "%");
System.out.println("14's\t\t" + probabilityOfDice + "%");
System.out.println("15's\t\t" + probabilityOfDice + "%");
System.out.println("16's\t\t" + probabilityOfDice + "%");
System.out.println("17's\t\t" + probabilityOfDice + "%");
System.out.println("18's\t\t" + probabilityOfDice + "%");
System.out.println("19's\t\t" + probabilityOfDice + "%");
System.out.println("20's\t\t" + probabilityOfDice + "%");
System.out.println("21's\t\t" + probabilityOfDice + "%");
System.out.println("22's\t\t" + probabilityOfDice + "%");
}
}

最佳答案

据我所知,您不需要第二个 for 循环。每次掷骰子时,您都会计算总和并根据总和增加计数。

您需要使用单独的变量来计算每个总和的概率。

例如

Probability of count2 = (count2/number of rolls);
Probability of count3 = (count2/number of rolls);

使用单独的变量计算计数概率

试试这个代码

import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11= 0;
int count12= 0;
int count13 = 0;
int count14 = 0;
int count15 = 0;
int count16 = 0;
int count17 = 0;
int count18 = 0;
int count19 = 0;
int count20 = 0;
int count21 = 0;
int count22 = 0;
int die1 = 0, die2 = 0;
int rolls = 0;
int actualDiceSum;
double probabilityOfDice = 0.0;

System.out.print("Number of Rolls: ");
rolls = in.nextInt();

for(int timesRolled = 0; timesRolled < rolls; timesRolled++)
{
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;

if(actualDiceSum == 2){
count2++;

}
else if(actualDiceSum == 3){
count3++;

}
else if(actualDiceSum == 4){
count4++;

}
else if(actualDiceSum == 5){
count5++;

}
else if(actualDiceSum == 6){
count6++;

}
else if(actualDiceSum == 7){
count7++;

}
else if(actualDiceSum == 8){
count8++;

}
else if(actualDiceSum == 9){
count9++;

}
else if(actualDiceSum == 10){
count10++;

}
else if(actualDiceSum == 11){
count11++;

}
else if(actualDiceSum == 12){
count12++;

}
else if(actualDiceSum == 13){
count13++;

}
else if(actualDiceSum == 14){
count14++;

}
else if(actualDiceSum == 15){
count15++;

}
else if(actualDiceSum == 16){
count16++;

}
else if(actualDiceSum == 17){
count17++;

}
else if(actualDiceSum == 18){
count18++;

}
else if(actualDiceSum == 19){
count19++;

}
else if(actualDiceSum == 20){
count20++;

}
else if(actualDiceSum == 21){
count21++;

}
else if(actualDiceSum == 22){
count22++;

}
}
}


System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + count2/rolls + "%");
System.out.println("3's\t\t" + count3/rolls + "%");
System.out.println("4's\t\t" + count4/rolls + "%");
System.out.println("5's\t\t" + count5/rolls + "%");
//and so on...
}
}

关于java - (Java) 使用嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com