예제 #1
0
        public Answer GetAnswer(Question question, int userAnswer)
        {
            int rightAnswer = 0; //正确答案
            string answerStatusString; //答题状态字符串

            string questionString; //问题字符串

            //根据生成的问题中的不同的运算符,计算问题的正确答案
            switch (question.OperatorString)
            {
                case "+":
                    rightAnswer = question.Operand1 + question.Operand2;
                    break;
                case "-":
                    rightAnswer = question.Operand1 - question.Operand2;
                    break;
                case "*":
                    rightAnswer = question.Operand1*question.Operand2;
                    break;
                case "/":
                    rightAnswer = question.Operand1/question.Operand2;
                    break;
                default:
                    break;
            }

            //答题记录中的问题字符串(如 “10 + 32”)
            questionString = question.Operand1 + " " + question.OperatorString + " " + question.Operand2;

            //通过判断用户答案与正确答案是否一致,来设置答题状态为“√”还是“×”
            if (userAnswer == rightAnswer)
            {
                answerStatusString = "√";
            }
            else
            {
                answerStatusString = "×";
            }

            Answer answer = new Answer(); //创建一个答题记录对象(Answer对象)
            answer.QuestionString = questionString;
            answer.RightResult = rightAnswer;
            answer.UserAnswer = userAnswer;
            answer.Status = answerStatusString;

            return answer; //返回answer对象
        }
예제 #2
0
        public Question GetQuestion()
        {
            Question question = new Question(); //创建一个Question类型的对象
            question.Operand1 = GenerateOperand(); //随机生成操作数1
            question.Operand2 = GenerateOperand(); //随机生成操作数2
            question.OperatorString = GenerateOperatorString(); //随机生成运算符

            return question; //返回question对象
        }