[英]Dice Roll Simulation
所以我迷路了。 這是目標:
問多少次擲一個六面骰子。 隨機擲骰子。 打印出每個數字在1到6之間出現了多少次
這個循環事情是否正確? 我應該從什么開始我真的很困惑
while(dice > 0)
{
rolldice = gen.nextInt(6) + 1; //(1-6)
if (rolldice == 1)
{
one++;
}
else if (rolldice == 2)
{
two++;
}
else if (rolldice == 3)
{
three++;
}
else if (rolldice == 4)
{
four++;
}
else if (rolldice == 5)
{
five++;
}
else if (rolldice == 6)
{
six++;
}
}
我假設骰子整數是用戶的輸入。 如果每次迭代都減少骰子,則將骰子滾動(遍歷循環),次數取決於用戶要求。
不知道您是否已實現它,以便用戶可以輸入許多骰子,如果您需要幫助,請參閱“掃描器”。
Scanner in = new Scanner(System.in);
Random gen = new Random();
int rollDice;
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
System.out.println("How many die do you want to roll: ");
int dice = in.nextInt();
while(dice > 0)
{
rolldice = gen.nextInt(6) + 1; //(1-6)
if (rolldice == 1)
{
one++;
}
else if (rolldice == 2)
{
two++;
}
else if (rolldice == 3)
{
three++;
}
else if (rolldice == 4)
{
four++;
}
else if (rolldice == 5)
{
five++;
}
else if (rolldice == 6)
{
six++;
}
dice--;
}
現在只需打印出變量即可完成操作!
我也可以使用switch
語句來做到這一點,但是我將讓您弄清楚如果要使用的內容該如何進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.