首页 > 解决方案 > 变量不覆盖 C#

问题描述

我正在尝试设置一个“分数”变量,它会在我每次调用时添加一些分数时一直改变。然而,我的代码加了一些点,但如果我再次调用它,它会重新设置值并加起来。这意味着我想将一个点加到 0,它会打印 1,如果我想再次添加它,它将再次打印 1 个点。这是我的代码:

    class StatusManage
    {
        public int score;

        public void increase()
        {
            score++;
        }
    }

    class QuestionManage
    {
        StatusManage status = new StatusManage();
        public int limit = 4;

        public void choose()
        {
            Console.WriteLine("Your score: {0}", status.score);
            Questions q = new Questions();
            if (status.score >= limit)
            {
                Console.WriteLine("The end! Congratulation");
                status.score = 0;
            }
            switch (status.score)
            {
                case 0:
                    q.question1();
                    break;
                case 1:
                    q.question2();
                    break;
                case 2:
                    q.question3();
                    break;
            }
        }
        public void getAnswer(char correctAnswer)
        {
            Console.Write("Type your answer: ");
            ConsoleKeyInfo answer = Console.ReadKey();
            Console.WriteLine();
            if (char.ToUpper(answer.KeyChar) == correctAnswer)
            {
                Console.WriteLine("You are correct!");
                status.increase();
                choose();
            }
            else
            {
                Console.WriteLine("Incorrect answer, try it again...");
                getAnswer(correctAnswer);
            }
        }

    }
class Questions
    {
        QuestionManage questionManage = new QuestionManage();

        public void question1()
        {
            // Questions
            // Q1
            text = "How do we declare whole number variables in C#";
            string question = text + "?";
            Console.WriteLine(question);

            letter = 1;
            text = "int 1x = 10";
            string choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 2;
            text = "int x = 10";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 3;
            text = "float x = 10f";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 4;
            text = "string x = \"10\"";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);
            Console.WriteLine();

            questionManage.getAnswer('B');
        }

        public void question2()
        {
            // Questions
            // Q2
            text = "In what country is Stockholm";
            string question = text + "?";
            Console.WriteLine(question);

            letter = 1;
            text = "Germany";
            string choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 2;
            text = "Denmark";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 3;
            text = "Austria";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 4;
            text = "Sweden";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);
            Console.WriteLine();

            questionManage.getAnswer('D');
        }

        public void question3()
        {
            // Questions
            // Q2
            text = "Check";
            string question = text + "?";
            Console.WriteLine(question);

            letter = 1;
            text = "Correct";
            string choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 2;
            text = "Incorrect";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 3;
            text = "Incorrect";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);

            letter = 4;
            text = "Incorrect";
            choice = $"{choicePrefix[letter - 1]}) {text}";
            Console.WriteLine(choice);
            Console.WriteLine();

            questionManage.getAnswer('A');
        }

        public static string text;
        public static int letter;
        public static char[] choicePrefix = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
    }

这里是控制台中的结果: 控制台结果

谢谢你的帮助

标签: c#

解决方案


推荐阅读