C#家庭作业掷骰子游戏
我正在学习c#课程,我有很多有趣的学习,但是我被困在这一项作业中——我必须模拟掷一个6面骰子/骰子500次(用户必须输入掷骰子的数量),同时显示每面骰子的频率(1-6)和掷骰子的百分比。此外,我不能使用数组。 因此,首先我要做的是启动一个bool来循环我的代码,因为我必须询问用户是否想要再次滚动。然后我为dice1创建了一个变量,并将其设置为1-6之间的随机数。我尝试将一个变量设置为与dice1相等的控制台读线,让用户输入数字。这几乎就是我被卡住的地方,无法前进。 我知道你们中的许多人比我知识渊博,所以如果有人能给我一些建议就太好了 以下是我目前掌握的情况:C#家庭作业掷骰子游戏,c#,dice,C#,Dice,我正在学习c#课程,我有很多有趣的学习,但是我被困在这一项作业中——我必须模拟掷一个6面骰子/骰子500次(用户必须输入掷骰子的数量),同时显示每面骰子的频率(1-6)和掷骰子的百分比。此外,我不能使用数组。 因此,首先我要做的是启动一个bool来循环我的代码,因为我必须询问用户是否想要再次滚动。然后我为dice1创建了一个变量,并将其设置为1-6之间的随机数。我尝试将一个变量设置为与dice1相等的控制台读线,让用户输入数字。这几乎就是我被卡住的地方,无法前进。 我知道你们中的许多人比我知识渊
static void Main(string[] args)
{
//Initiate Looping Seqence
bool choice = true;
while (choice)
{
//Display Introduction and Instructions
Console.WriteLine("Welcome To The Dice Game!");
Console.WriteLine("This Program will simulate rolling a die and will track");
Console.WriteLine("the Frequency each value is rolled Then Display a Session Summary");
Console.WriteLine("Press any key to continue.....");
Console.ReadKey();
Console.Clear();
Console.WriteLine("How many times would you like to roll the die?");
Random randomNum = new Random();
int dice1;
dice1 = randomNum.Next(1, 7);
int choice2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine(dice1);
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
int s6 = 0;
Console.WriteLine("Would you Like to Run This Program Again?");
Console.WriteLine("Type \"yes\" to re-run or Type \"no\" to exit?");
Console.ReadKey();
string option;
option = Console.ReadLine();
}
}
// public static double RollDice()
//{
// was not sure if to create a dice roll method
//}
}
}
下面是一些模拟滚动的代码&获取数字弹出的次数
int rollAmount = 500;
int dice;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
int s6 = 0;
Random random = new Random();
for(int i = 0; i <= rollAmount; i++){
dice = random.next(1,7);
Console.Writeline("Roll " + i + ": " + dice.ToString());
if(dice == 1) s1++;
else if(dice == 2) s2++;
else if(dice == 3) s3++;
...
}
Console.Writeline("1 Percentage: " + ((s1 / rollAmount) * 100) + "%");
Console.Writeline("2 Percentage: " + ((s2 / rollAmount) * 100) + "%");
Console.Writeline("3 Percentage: " + ((s3 / rollAmount) * 100) + "%");
...
Console.Writeline("Done");
int rollmount=500;
整数骰子;
int s1=0;
int s2=0;
int s3=0;
int s4=0;
int s5=0;
int s6=0;
随机=新随机();
对于(inti=0;i有一个休息,取我的代码ヽ(^o^)丿
类程序{
#区域lambda快捷方式
静态动作警告=m=>{
Console.ForegroundColor=ConsoleColor.Yellow;
控制台写入线(m);
};
静态操作信息=m=>{
Console.ForegroundColor=ConsoleColor.White;
控制台写入线(m);
};
静态操作WriteQuery=m=>{
Console.ForegroundColor=ConsoleColor.Gray;
控制台。写入(m);
};
静态动作特征线=Console.WriteLine;
#端区
静态void Main(字符串[]参数){
Console.Title=“骰子游戏”;
while(true){
bool@continue=RunDiceGame();
如果(!@继续){
打破
}
Console.Clear();
}
}
静态bool RunDiceGame(){
//显示介绍和说明
信息(“欢迎参加骰子游戏!”);
信息(“此程序将模拟掷骰子并跟踪结果”);
特征线();
WriteQuery(“骰子滚动计数:”);
int rollCount=ReadInt32();
WriteQuery(“随机数生成器种子:”);
Random rng=新随机(ReadInt32());
特征线();
特征线();
//将骰子值映射到骰子计数
var diceValues=新字典((int)rollCount);
对于(int i=0;i<6;i++){
添加(i,0);
}
//掷骰子
for(int i=0;i
欢迎来到SO!在这里发帖时需要考虑以下几点:你到底有什么问题?问题是什么?你还可以阅读如何更好地提问。本网站真正关注的是更具体的问题-你至少需要解释一下你认为该程序应该如何工作,并尝试实施这是一个逻辑。如果你填得太满了,它就不起作用了,那么请回来寻求帮助。我建议用一个“Roll”方法创建一个骰子类,生成一个带有“Dice side”数字的随机值看看这里:在末尾插入一个readline,这样控制台窗口就不会关闭它的c#,它也不会关闭。编辑:暗示它是windows窗体应用程序我更喜欢#ReadKey(true)
@i'KeiAmuquadoh当它们启动静态void Main(字符串[]args)时
它只是对我读控制台应用程序而不是WinForms。你完全正确,我读了C#并只是假设。我的错。一个投了反对票的忍者出现了,并在我宝贵的帖子上获得了致命一击!还剩下1 HP…感谢老兄!这真的很有帮助。我只需要换几件事。
class Program {
#region lambda shortcuts
static Action<object> Warn = m => {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(m);
};
static Action<object> Info = m => {
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(m);
};
static Action<object> WriteQuery = m => {
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(m);
};
static Action BreakLine = Console.WriteLine;
#endregion
static void Main(string[] args) {
Console.Title = "The Dice Game";
while(true) {
bool @continue = RunDiceGame();
if(!@continue) {
break;
}
Console.Clear();
}
}
static bool RunDiceGame() {
//Display Introduction and Instructions
Info("Welcome To The Dice Game!");
Info("This Program will simulate rolling a dice and keeps track of the results");
BreakLine();
WriteQuery("Dice roll count: ");
int rollCount = ReadInt32();
WriteQuery("Random number generator seed: ");
Random rng = new Random(ReadInt32());
BreakLine();
BreakLine();
//map dice value to dice count
var diceValues = new Dictionary<int, int>((int)rollCount);
for(int i = 0; i < 6; i++) {
diceValues.Add(i, 0);
}
//roll dice
for(int i = 0; i < rollCount; i++) {
int diceValue = rng.Next(6); //roll 0..5
diceValues[diceValue]++;
}
//print results
for(int i = 0; i < 6; i++) {
int valueRollAbsolute = diceValues[i];
double valueRollCountRelative = valueRollAbsolute / (double) rollCount;
Info(string.Format("Value: {0}\tCount: {1:0,0}\tPercentage: {2:0%}", i + 1, valueRollAbsolute, valueRollCountRelative));
}
BreakLine();
BreakLine();
WriteQuery("Type 'yes' to restart, 'no' to exit.");
BreakLine();
return ReadYesNo();
}
#region console read methods
static int ReadInt32() {
while(true) {
string input = Console.ReadLine();
try {
return Convert.ToInt32(input);
} catch(FormatException) {
Warn("Not a number: " + input);
}
}
}
static bool ReadYesNo() {
while(true) {
string option = Console.ReadLine();
switch(option.ToLowerInvariant()) {
case "yes":
return true;
case "no":
return false;
default:
Warn("Invalid option: " + option);
break;
}
}
}
#endregion
}