c# Roll Dice模拟到return每边落地多少次
c# Roll Dice simulation to return how many times every side landed
我的第一个 post 在这里!几天前我开始学习如何编程,并选择了一个掷骰子项目。我想我已经完成了它的主要部分,但我的问题来自:
每面打印多少次(也许将它们存储在一个数组中)?
奖励:打印出每一面出现的次数百分比(四舍五入到小数点后两位)
请记住时间和面是用户输入的,因此可以是例如50 次 x 35 边。
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
namespace Dice_roll
{
class Program
{
static void Main(string[] args)
{
static int getRandomNumber(int min, int max)
{
Random randomRoll = new Random();
lock (randomRoll)
{
return randomRoll.Next(min, max);
}
}
int sides, rollTimes, i;
i = 0;
bool boolCheckSides = false;
bool boolCheckRolls = false;
Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
do
{
boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
if (boolCheckSides == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckSides == false);
Console.WriteLine("Now select how many times you want it to roll: \n");
do
{
boolCheckRolls = Int32.TryParse(Console.ReadLine(), out rollTimes);
if (boolCheckRolls == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckRolls == false);
do
{
Console.WriteLine("Rolling... " + getRandomNumber(1, sides));
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
int[] arr = new int[rollTimes];
}
}
}
很抱歉,如果我的代码很乱或者什么的,我还是个初学者。
嗯,我假设你的代码还没有完成,所以我只是分享一些我的意见。
1- 您在主函数中添加了一个方法 getRandomNumber。此方法不会编译,应移出 main 函数的范围。 main函数的作用域是下面代码中大括号“{}”之间的任意代码
static void main(string[] args){}
只是一个小提示,尝试在 C# 中使用大写字母命名方法,但 main 函数除外,因为这是一种命名约定;-)
2- 在开始为它们赋值之前,您在声明所需变量(boolCheckSides、boolCheckRolls、i)方面做得很好。但是,您还需要在使用之前而不是之后声明数组,因此您的代码应如下所示
//Declare the Array
int[] arr = new int[rollTimes];
do
{
int temp = getRandomNumber(1, sides);
Console.WriteLine("Rolling... " + temp);
//Save the value acquired from this roll in the array
arr[i] = temp;
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
然后你就可以轻松地摆弄数组并找到任意数字的频率。我建议您参考此 post if you want to find the short answer, and refer to this post Frequency of Numbers in a 1D Array 以获得详细答案。请注意,第二个 post 使用的是 C++ 语言,但它仍然显示相同的逻辑。
3- 一个小小的建议,请尽量避免 "do...while" 循环。写的时候要多加注意,很容易让你陷入死循环。
我建议你使用字典,以边(1 到 X)作为键和它出现的次数。
class Program
{
Dictionary<int, int> RollDice = new Dictionary<int, int>();
static void Main(string[] args)
{
:
Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
do
{
boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
if (boolCheckSides == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckSides == false);
//Initialize the dictionary with key and value
for(int i = 1; i<=sides;i++)
RollDice.Add(i, 0);
:
i = 0;
do
{
var value = getRandomNumber(1, sides + 1)
Console.WriteLine("Rolling... " + value);
RollDice[i]++;
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
您可以通过遍历字典来显示您的统计数据:
foreach (var pair in RollDice)
{
Console.WriteLine($"DiceSide:{pair.Key} , appears: {pair.Value}, probability: {(float)pair.Value/(float)rollTimes:0.00}");
}
我的第一个 post 在这里!几天前我开始学习如何编程,并选择了一个掷骰子项目。我想我已经完成了它的主要部分,但我的问题来自:
每面打印多少次(也许将它们存储在一个数组中)? 奖励:打印出每一面出现的次数百分比(四舍五入到小数点后两位)
请记住时间和面是用户输入的,因此可以是例如50 次 x 35 边。
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
namespace Dice_roll
{
class Program
{
static void Main(string[] args)
{
static int getRandomNumber(int min, int max)
{
Random randomRoll = new Random();
lock (randomRoll)
{
return randomRoll.Next(min, max);
}
}
int sides, rollTimes, i;
i = 0;
bool boolCheckSides = false;
bool boolCheckRolls = false;
Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
do
{
boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
if (boolCheckSides == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckSides == false);
Console.WriteLine("Now select how many times you want it to roll: \n");
do
{
boolCheckRolls = Int32.TryParse(Console.ReadLine(), out rollTimes);
if (boolCheckRolls == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckRolls == false);
do
{
Console.WriteLine("Rolling... " + getRandomNumber(1, sides));
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
int[] arr = new int[rollTimes];
}
}
}
很抱歉,如果我的代码很乱或者什么的,我还是个初学者。
嗯,我假设你的代码还没有完成,所以我只是分享一些我的意见。
1- 您在主函数中添加了一个方法 getRandomNumber。此方法不会编译,应移出 main 函数的范围。 main函数的作用域是下面代码中大括号“{}”之间的任意代码
static void main(string[] args){}
只是一个小提示,尝试在 C# 中使用大写字母命名方法,但 main 函数除外,因为这是一种命名约定;-)
2- 在开始为它们赋值之前,您在声明所需变量(boolCheckSides、boolCheckRolls、i)方面做得很好。但是,您还需要在使用之前而不是之后声明数组,因此您的代码应如下所示
//Declare the Array
int[] arr = new int[rollTimes];
do
{
int temp = getRandomNumber(1, sides);
Console.WriteLine("Rolling... " + temp);
//Save the value acquired from this roll in the array
arr[i] = temp;
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
然后你就可以轻松地摆弄数组并找到任意数字的频率。我建议您参考此 post
3- 一个小小的建议,请尽量避免 "do...while" 循环。写的时候要多加注意,很容易让你陷入死循环。
我建议你使用字典,以边(1 到 X)作为键和它出现的次数。
class Program
{
Dictionary<int, int> RollDice = new Dictionary<int, int>();
static void Main(string[] args)
{
:
Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
do
{
boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
if (boolCheckSides == false)
{
Console.WriteLine("Invalid number.. \n");
}
} while (boolCheckSides == false);
//Initialize the dictionary with key and value
for(int i = 1; i<=sides;i++)
RollDice.Add(i, 0);
:
i = 0;
do
{
var value = getRandomNumber(1, sides + 1)
Console.WriteLine("Rolling... " + value);
RollDice[i]++;
i = i + 1;
System.Threading.Thread.Sleep(500);
} while (i < rollTimes);
您可以通过遍历字典来显示您的统计数据:
foreach (var pair in RollDice)
{
Console.WriteLine($"DiceSide:{pair.Key} , appears: {pair.Value}, probability: {(float)pair.Value/(float)rollTimes:0.00}");
}