C# 我在玩骰子游戏时遇到问题,每次用户玩游戏时我都需要增加一个值,然后将其打印回给他们

C# I'm having trouble with a dice game where I need to increment a value every time a user plays a game and then print it back to them

我有一个包含两个骰子的骰子游戏,发生的情况是一个游戏将通过,然后将询问用户是否要再次玩。例如,如果他们说了 3 次“是”,那么当他们最后说“不”退出游戏时,他们会得到一个输出,告诉他们他们玩了多少次游戏。我在想出它的代码时遇到了问题。

我没有太多使用参数和 return 类型的经验(这是初学者的作业),但我目前有一个加 1 的计数器。问题是它从 0 开始到 1 , 然后留在那里。

这是我在游戏中运行的代码:

namespace MajorAssignment1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
            Console.WriteLine();
            EvenOrOdds();

        }

        public static void EvenOrOdds()
        {

            Random rnd = new Random();

            int die1 = rnd.Next(1, 10);
            int die2 = rnd.Next(1, 10);

            Console.WriteLine("Die 1 = {0} and Die 2 = {1}", die1, die2);
            Console.WriteLine();
            Console.WriteLine("You Rolled {0} and {1}", die1, die2);
            Console.WriteLine();

            if ((die1 + die2) % 2 == 0)
            {
                Console.WriteLine("Evens are better than odd.");
                Console.WriteLine();
            }
            if ((die1 + die2) % 2 > 0 )
            {
                Console.WriteLine("Odds are still cool.");
                Console.WriteLine();
            }


            Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
            String UserInput = Console.ReadLine();
            var Counter = 0;
            if (UserInput == "YES")
            {
                EvenOrOdds();

                Counter = Counter + 1;
            }
            else
            {
                Console.WriteLine("The number of times the dice was thrown is:" + Counter);
                Console.WriteLine();
            }



        }

        public static void Outro()
        { 
                Console.WriteLine("Thanks for playing! Come again!");
                Console.WriteLine();

        }
    }
}

通过从自身内部调用 EvenOrOdds() 以便 "play again",您实际上是在创建对该函数的递归调用。

您正在调用的每个 EvenOrOdds() 实例的范围内重新定义 Counter,导致 Counter 始终以 1 结束。

一个简单的选择是将 Counter 的定义移出到 class 级变量中,这意味着它会在您对 EvenOrOdds() 的所有调用之间共享

class MainClass
    {
        //class-level static variable
        private static int Counter;

        public static void Main(string[] args)
        {
            Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
            Console.WriteLine();
            EvenOrOdds();

        }

// rest of code here

这意味着您可以在 EvenOrOdds() 代码中删除 Counter 的定义。现在,当您增加 Counter 时,它正在更新 class 级变量,这将导致您预期的 Counter 行为。

Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
String UserInput = Console.ReadLine();

if (UserInput == "YES")
{
    //increment the counter first
    Counter = Counter + 1;

    //then call the method again for a new game
    EvenOrOdds();

}

您也可以更改 "Counter = Counter + 1;" 行,您使用内联 ++ 增量运算符:"Counter++ ;" 将做同样的事情。

注意:还有其他方法可以更好地实现这种类型的"play again"功能,例如使用循环等,但无需重写您已经拥有的内容显着完成我上面的建议将足以作为实现你想做的事情的微小变化。祝你好运!

编辑:更新为先递增计数器,然后再次调用 EventOrOdds() - 这会导致每次玩游戏时计数器都正确递增。

您的代码存在问题,您正在递归调用 EvenOrOdds() ,并且 counter 从不递增。而且,你把简单的事情复杂化了,我简化了一些。

工作代码:

using System;
public class diceCounter
{
    public static void Main(string[] args)
    {
        String UserInput;
        int Counter =1;
        Console.WriteLine("Hey! Welcome to Ray's Dice Game! Let's Start");
        Console.WriteLine();
        do
        {
            EvenOrOdds();
            Console.WriteLine("Do you want to play again? Please enter in all caps YES or NO");
            UserInput = Console.ReadLine();
            if (UserInput.Equals("YES"))
            {
                Counter++;
                EvenOrOdds();
            }
        }while(!(UserInput.Equals("NO")));

        Console.WriteLine("The number of times the dice was thrown is: " + Counter);
        Outro();
    }

    public static void EvenOrOdds()
    {
        Random rnd = new Random();
        int die1 = rnd.Next(1, 10);
        int die2 = rnd.Next(1, 10);

        Console.WriteLine("Die 1 = {0} and Die 2 = {1}", die1, die2);
        Console.WriteLine();
        Console.WriteLine("You Rolled {0} and {1}", die1, die2);
        Console.WriteLine();

        if ((die1 + die2) % 2 == 0)
        {
            Console.WriteLine("Evens are better than odd.");
            Console.WriteLine();
        }
        if ((die1 + die2) % 2 > 0 )
        {
            Console.WriteLine("Odds are still cool.");
            Console.WriteLine();
        }

    }

    public static void Outro()
    { 
        Console.WriteLine("\nThanks for playing! Come again!\n");
    }
}

我可以想出两种解决方案

1) 在class级别使用私有变量。请记住在您的方法

中删除计数器的定义
class MainClass {
private static int Counter = 0;
...
}

2) 向您的方法发送一个 ref 参数

public static void EvenOrOdds(ref int counter)

主要是 事件或赔率(计数器)。你的递归也是如此