我被分配了一个项目,我需要在其中创建一个程序,利用嵌套循环,用户可以输入一对骰子的边数和滚动次数。然后程序应该计算滚动某些数字组合的概率。该程序有点简单,因为它为您提供了所有代码,您只需要按正确的顺序放置它。我几天来一直在这个小工作,但我似乎无法使它工作。这就是我对代码所拥有的:
/**
* This program roll a dice with a selected amount of sides a selected amount of times.
*
* Timothy Pierce
* 1/5/2016
*
*/
import java.util.Scanner;
import java.util.Random;
class DiceProbability
{
public static void main (String[] args)
{
//Initalizing variables
int numRolls = 0;
int numSides = 0;
int match = 0;
int sum = 0;
//Accepting User Input
Scanner in = new Scanner(System.in);
System.out.print("Please enter the number of rolls: ");
numRolls = in.nextInt();
System.out.print("Please enter the number of sides on a dice: ");
numSides = in.nextInt();
System.out.print("\n Sum of Dice \t Probability \n");
//Begin Loops
for(int roll = 0; roll < numRolls; roll++)
{
for(sum = 2; sum <= (numSides*2); sum++)
{
int die1 = 0;
int die2 = 0;
Random randNum = new Random();
die1 = randNum.nextInt(numSides)+1;
die2 = randNum.nextInt(numSides)+1;
if(die1 + die2 == sum)
{
match = 0;
match++;
}
double probability = 0.0;
probability = (double)match / numRolls * 100;
System.out.println(" " + sum + "\t\t" + probability);
}
}
}//End Method
}//End Class
所有代码都在那里,它只是没有正确的顺序。输出的格式应该是:行要求卷数,行要求边数,行标题有一列骰子和另一列豁免概率,然后循环应该“滚动骰子”并预测每个数字的概率。 IE它将有两列:一列是sum,另一列是概率。然后它应按顺序下降2,3,4,5,6等,并在其旁边的下一列中具有相应的概率。非常感谢所有帮助!
答案 0 :(得分:0)
也许你的代码有问题,而不仅仅是订单。所以我试着写另一个,尝试一下。
/**
* This program roll a dice with a selected amount of sides a selected amount of times.
*
* Fergus Wang
* 1/6/2016
*
*/
import java.util.Scanner;
import java.util.Random;
class DiceProbability
{
public static void main (String[] args)
{
//Initalizing variables
int numRolls = 0;
int numSides = 0;
int match = 0;
int sum = 0;
//Accepting User Input
Scanner in = new Scanner(System.in);
System.out.print("Please enter the number of rolls: ");
numRolls = in.nextInt();
System.out.print("Please enter the number of sides on a dice: ");
numSides = in.nextInt();
System.out.print("\n Sum of Dice \t Probability \n");
// A list to save match times.
int[] matchList = new int[numSides*2+1];
// creat a Random tool.
Random randNum = new Random();
// Begin Loops
for(int roll = 0; roll < numRolls; roll++)
{
int die1 = 0;
int die2 = 0;
die1 = randNum.nextInt(numSides)+1;
die2 = randNum.nextInt(numSides)+1;
int tempSum = die1 + die2;
// check sum value.
for(sum = 2; sum <= (numSides*2); sum++)
{
if(tempSum == sum)
{
matchList[sum-2]++;
}
}
}
for(int index = 0; index<numSides*2-1; index++){
double probability = 0.0;
probability = (double)matchList[index] / numRolls * 100;
System.out.println(" " + (index+2) + "\t\t" + probability);
}
}//End Method
}//End Class
答案 1 :(得分:0)
我认为代码中的问题是:&#34; die1&#34;和&#34; die2&#34;必须计算每个卷,而不是每个循环的&#34;总和&#34;值。 &#34;匹配&#34;必须为&#34; sum&#34;的每个循环重置一个值。即使`die1 + die2 == sum&#39;也不行。根据解释,我只是重新排序你的代码(没有改变任何代码行)。 这是我的实施:
import java.util.Scanner;
import java.util.Random;
class DiceProbability
{
public static void main (String[] args)
{
//Initalizing variables
int numRolls = 0;
int numSides = 0;
int match = 0;
int sum = 0;
//Accepting User Input
Scanner in = new Scanner(System.in);
System.out.print("Please enter the number of rolls: ");
numRolls = in.nextInt();
System.out.print("Please enter the number of sides on a dice: ");
numSides = in.nextInt();
System.out.print("\n Sum of Dice \t Probability \n");
//Begin Loops
for(int roll = 0; roll < numRolls; roll++)
{
int die1 = 0;
int die2 = 0;
Random randNum = new Random();
die1 = randNum.nextInt(numSides)+1;
die2 = randNum.nextInt(numSides)+1;
for(sum = 2; sum <= (numSides*2); sum++)
{
match = 0;
if(die1 + die2 == sum)
{
match++;
}
double probability = 0.0;
probability = (double)match / numRolls * 100;
System.out.println(" " + sum + "\t\t" + probability);
}
}
}//End Method
}//End Class
希望这有帮助!