c#骰子游戏的实现

c#骰子游戏的实现,c#,dice,C#,Dice,我一直在思考如何实现代码,所以当它编译成一个计数器时,它只会显示一个计数器,而不是一个计数器的复数形式。我需要关于如何在我下面给出的代码中实现这段代码的帮助 namespace ReferralDG { class Program { static void Main(string[] args) { Console.WriteLine("Even Minus Odd Game: ");

我一直在思考如何实现代码,所以当它编译成一个计数器时,它只会显示一个计数器,而不是一个计数器的复数形式。我需要关于如何在我下面给出的代码中实现这段代码的帮助

namespace ReferralDG
{
    class Program
    {
        static void Main(string[] args)
        {
                Console.WriteLine("Even Minus Odd Game: ");
                Random rand = new Random();
                bool PlayAgain = false;
                do
                {
                    int numberOfDice = 0;
                    int totalOfDiceRolled = 0;
                    while (numberOfDice > 10 || numberOfDice < 3)
                    {
                        Console.WriteLine("How many dice do you wish to play     with? (Between 3 and 10?)");
                    numberOfDice = Convert.ToInt32(Console.ReadLine());
                    if (numberOfDice > 10 || numberOfDice < 3)
                    {
                        Console.WriteLine("Please enter the correct number
                    }
                    }

                    Console.Write("Dice rolls: ");
                    int evenTotal = 0;
                    int oddTotal = 0;
                    for (int i = 0; i < numberOfDice; i++)
                    {                    
                        int diceRoll = rand.Next(1, 7);
                        if (diceRoll % 2 == 0)
                        {
                            evenTotal += diceRoll;
                        }
                        else
                        {
                            oddTotal += diceRoll;
                        }
                        totalOfDiceRolled += diceRoll;
                        Console.Write(diceRoll + " ");
                    }
                    int counters = evenTotal - oddTotal;
                    Console.WriteLine();
                    if (counters > 0)
                    {
                        Console.WriteLine("You take " + counters + "     counters.");
                }
                else if (counters < 0)
                {
                    Console.WriteLine("You give " + Math.Abs(counters) + " counters.");
                }
                else if (evenTotal == oddTotal)
                {
                    Console.WriteLine("Even total is equal to odd total");
                }
                else
                {
                    Console.WriteLine("No counters this game");
                }
                Console.WriteLine("Would you like to play again? (Y/N): ");
                string playAgain = Console.ReadLine().ToUpper();
                if (playAgain == "Y")
                {
                    PlayAgain = true;
                }
                else
                {
                    PlayAgain = false;
                }
            } while (PlayAgain);
       }
  }
namespace ReferralDG
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台。WriteLine(“偶数减去奇数游戏”);
Random rand=新的Random();
bool playreach=false;
做
{
int numberOfDice=0;
int TOTALOFDICHELLED=0;
而(numberOfDice>10 | | numberOfDice<3)
{
控制台。WriteLine(“你想玩多少个骰子?(3到10之间”);
numberOfDice=Convert.ToInt32(Console.ReadLine());
如果(numberOfDice>10 | | numberOfDice<3)
{
Console.WriteLine(“请输入正确的号码
}
}
控制台。写(“掷骰子”);
int evenTotal=0;
int-oddtoal=0;
for(int i=0;i0)
{
控制台.WriteLine(“您使用“+计数器+”计数器”);
}
else if(计数器<0)
{
WriteLine(“您给出”+Math.Abs(counters)+“counters”);
}
else if(EventTotal==oddTotal)
{
Console.WriteLine(“偶数合计等于奇数合计”);
}
其他的
{
控制台.WriteLine(“本游戏无计数器”);
}
Console.WriteLine(“您想再次播放吗?(Y/N):”;
string play=Console.ReadLine().ToUpper();
如果(再次播放==“Y”)
{
再次播放=正确;
}
其他的
{
再次播放=错误;
}
}边玩边玩;
}
}

}

您可以在块中添加额外的

if (counters > 1) {
    Console.WriteLine("You take " + counters + " counters.");
}
else if (counters > 0) // or if you rather counters == 1
{
    Console.WriteLine("You take " + counters + " counter.");
}
else if (counters < -1)
{
    Console.WriteLine("You give " + Math.Abs(counters) + " counters.");
}
else if (counters < 0)
{
    Console.WriteLine("You give " + Math.Abs(counters) + " counter.");
}
else if (evenTotal == oddTotal)
{
    Console.WriteLine("Even total is equal to odd total");
}
else
{
    Console.WriteLine("No counters this game");
}

您可以使用if-shorthand
语句?valueIfTrue:valueIfFalse;
来设置正确的字符串值,并将所有内容放入这样的函数中

    private static void printCounters(int counters,bool take) {
        counters = Math.Abs(counters);
        string msg1 = take ? "You take " : "You give ";
        string msg2 = counters == 1 ? " counter." : " counters.";
        Console.WriteLine( msg1 + counters + msg2);
    }

这样做的好处是,您可以轻松地调用
打印计数器(counters,true)
,如果您选择,则调用
打印计数器(counters,give)
如果您选择

您好,欢迎来到StackOverflow Sam

所以当你把计数器写到屏幕上时,如果它们是一个计数器,你想让它写单数,如果它们多于1,那么写复数?我还假设我们想让它工作,不管计数器是正的还是负的

我们可以使用和,而不是在if语句中添加几个分支

在这种情况下,它看起来像这样:

 // Console.WriteLine("You take " + counters + "     counters.");
 Console.WriteLine(string.Format("You take {0}     counter{1}.", counters, Math.Abs(counters) == 1 ? "" : "s"));
这实际上只是一种编写多个“else-if”的方法,因为您仍然添加了一个分支语句。我的方法是,它是串联的,而不是在垂直方向上占用更多空间

如果你想变得更花哨,你也可以使用IIF设置“给予”或“接受”:


仔细阅读链接,看看什么最适合你!

不太清楚你在问什么。是`Console.WriteLine(“你拿”+counters+“counter”+counters==1?空:“s”)`你想要什么?所以基本上,当它编译它时,告诉“玩家”根据骰子thrwon给出或获取计数器,例如,如果它说“获取1个计数器”,我需要知道如何在不需要复数的情况下显示“获取1个计数器”,而不是计数器谢谢代码,你能解释一下我需要在代码中的什么位置放置它吗这是您的代码。我刚刚添加了一个附加的
if
。因此,只需将其替换为您的
if
else
block就可以了。因此,如果您添加了此代码,则应根据我的问题进行操作。如果我正确理解了您的问题,则为是。如果计数器为1,则输出将为单数“counter”,否则将为复数counters“yh这是正确的,它应该适用于给予或接受计数器???@Matz Reckeweg击败了我。我花了太长时间打印一本小说。我们基本上有相同的答案,他更易于重复使用。这很好,但它太花哨了,我无法包括在内,谢谢你的帮助
 // Console.WriteLine("You take " + counters + "     counters.");
 Console.WriteLine(string.Format("You take {0}     counter{1}.", counters, Math.Abs(counters) == 1 ? "" : "s"));
if (counters != 0)
{
    Console.WriteLine(string.Format("You {0} {1}     counter{2}.",
        counters > 0 ? "take" : "give",
        counters, 
        Math.Abs(counters) == 1 ? "" : "s"
        ));
}
else if (evenTotal == oddTotal)
{
    Console.WriteLine("Even total is equal to odd total");
}
else
{
    Console.WriteLine("No counters this game");
}