Переменная не переопределяет C# - PullRequest
0 голосов
/ 01 апреля 2020

Я пытаюсь установить переменную «оценка», которая будет меняться все время, я добавляю несколько баллов каждый раз, когда звоню. Мой код, однако, суммирует некоторые моменты, но если я вызову его снова, он возвращает значение и складывает. Это означает, что я хочу добавить одну точку к 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' };
    }

А вот и результат в консоли: Console result

Спасибо за любую помощь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...