C#家庭作业骰子游戏

时间:2015-03-06 16:16:10

标签: c# dice

我正在参加和进入c#课程并且我有很多有趣的学习,但是我不得不停留在这一项任务 - 我必须模拟滚动6面骰子/模具500次(用户必须输入卷数)显示每边的频率(1-6)和%roll。另外我不能使用数组。 所以,开始我所做的是启动一个bool来循环我的代码,因为我必须询问用户是否要再次滚动。然后我为dice1创建了一个变量,并将其设置为1-6之间的随机数。我尝试将变量设置为等于dice1的控制台readline,让用户输入数字。这几乎是我陷入困境并无法前进的地方。 我知道你们中许多人比我更有知识,所以如果有人能给我一些建议那就太棒了。

这是我到目前为止所拥有的:

    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            
      //}

    }

    }

2 个答案:

答案 0 :(得分:0)

休息一下,拿我的代码ヽ(^ o ^)丿

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
}

答案 1 :(得分:-2)

这是一些模拟卷筒和卷筒的代码。得到弹出数字的次数

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");