2

我正在尽我最大的努力做这个程序,即制作一个程序,让用户可以随意掷两个骰子,但是掷出的骰子不能显示为数字,而是显示为图像。

[o]

掷骰子为 1。

我还没有为程序制作循环代码,我只知道如何为滚动制作随机数,我只是不知道如何为图像制作数组列表并使代码实际使用图像而不是数字...如果你知道我的意思。

到目前为止,这是我的代码,感谢您的帮助!

        int[] DiceUno = new int[6];
        int[] DiceDos = new int[6];
        Random rnd = new Random();

        Console.WriteLine("This program will allow you to roll two dice");
        Console.WriteLine("\nAs many times as you want");
        Console.WriteLine("\n\nWhen you want to exit the program, please type (exit)");
        Console.WriteLine("\nPress any key to begin rolling");
        Console.Read();


        for (int i = 0; i < 1; i++)
        {
            int diceRoll = 0;
            diceRoll = rnd.Next(6);
            DiceUno[diceRoll]++;
            Console.WriteLine("Dice 1 is rolled a: {0}", diceRoll + 1);
            diceRoll = rnd.Next(6);
            DiceDos[diceRoll]++;
            Console.WriteLine("Dice 2 is rolled a: {0}", diceRoll + 1);

        }





    }
}

}

4

3 回答 3

5

这应该使用一些快速而肮脏的 LINQ 来工作。

var die = new Dictionary<int, string>
{
    { 1, "[     ]\n[  o  ]\n[     ]" }, //or a path to an image somewhere or anything you want
    { 2, "[     ]\n[ o o ]\n[     ]" },
    { 3, "[  o  ]\n[ o o ]\n[     ]" },
    { 4, "[ o o ]\n[     ]\n[ o o ]" },
    { 5, "[ o o ]\n[  o  ]\n[ o o ]" },
    { 6, "[ o o ]\n[ o o ]\n[ o o ]" },
};

do
{
    var shuffled = die.OrderBy(x => Guid.NewGuid()).Take(2);

    foreach (KeyValuePair<int, string> i in shuffled)
    {
        Console.WriteLine(i.Value);
        Console.WriteLine();
    }
} while (Console.ReadLine() != "(exit)");
于 2012-02-29T00:42:04.680 回答
0

为什么不这么简单

Dictionary<int, string> valueToDiceImage = new Dictionary<int, string>() 

{

 {0, "[0]"},

 {1, "[1]"},

 {2, "[2]"},

 {3, "[3]"},

 {4, "[4]"},

 {5, "[5]"},

 {6, "[6]"},

};

然后像这样使用它:

int diceRoll = rnd.next(6); 
System.Console.Write("User Rolled a " + valueToDiceImage[diceRoll] + "\n");
于 2012-02-29T00:29:22.060 回答
0

如果要输出文本而不是数字,请创建一个字符串数组:

string[] images = new string[]
    { "o", "oo", "ooo", "oooo", "ooooo", "oooooo" };

而不是 diceRoll + 1 在 Console.WriteLine 中放置图像 [diceRoll]:

Console.WriteLine("Dice 1 is rolled a: {0}", images[diceRoll]);

现在您可以使用图像,或者创建一个三行图像来显示出现在骰子上的数字(点空白)。

于 2012-02-29T00:35:29.043 回答