2

请看下面的代码

namespace Funny
{
    class QuesionsAndAnswers
    {
        private double firstNumber;
        private double secondNumber;
        private double userAnswer;
        private double computerAnswer;

        private string operators;

        private bool answerCorrect;
        private bool enableDouble;

        private double[] listOfNumbers;
        private string[] listOfOperators;

        private Random randomizer;

        private static QuesionsAndAnswers qa;

        private QuesionsAndAnswers()
        {
            randomizer = new Random();

            listOfNumbers = new double[] { 1,2,3,4,5,6,7,8,9 };
            listOfOperators = new string[] { "+", "-", "*", "/" };
        }

        public static QuesionsAndAnswers getQuesionsandAnswersInstance()
        {
            if (qa == null)
                qa = new QuesionsAndAnswers();
             return qa;
        }

        public string generateQuestions()
        {
            string result = "";

            operators = listOfOperators[randomizer.Next(listOfOperators.Length)];
            firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
            secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];

            if ((operators.Equals("/")) && (enableDouble == false))
            {
                while (firstNumber % secondNumber == 0)
                {
                    firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                }
                result = firstNumber + operators + secondNumber;
            }
            else if (operators.Equals("-") && (firstNumber<secondNumber))
            {
                while (firstNumber > secondNumber)
                {
                    firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                    secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                }
                result = firstNumber + operators + secondNumber;
            }
            return result;
        }

        public void setDoubleAnswers(bool check)
        {
            enableDouble = check;
        }
    }
}

在这里,在 generateQuestions() 方法中,我试图生成一些数学问题。

如果运算符在“-”中并且如果 firstNumber < secondNumber,则应重新生成数字(第一个数字和第二个数字)以确保答案不是负值。

并且,如果运算符是“/”并且布尔值为 false,它应该再次重新生成数字以确保答案(这意味着计算,例如 2/1 = 2)不包含任何浮点(这意味着它不应该产生像 2/3 这样的问题,因为答案包含浮点数)。

在我的尝试中,以上都没有发生。它仍然会产生意想不到的答案,或者有时什么也不会。这是为什么?请帮忙

请注意,我是一名 Java 开发人员,这是我的第一个主要 C# 项目。

4

6 回答 6

6

好吧,它有时什么也不返回,因为除了你正在捕捉的边缘情况之外,你从来没有设置result"".

而且你的两个while陈述都是错误的。它应该是:

while ((firstNumber % secondNumber) != 0)
:
while (firstNumber < secondNumber)

但是,为了提高效率,我不会这样做,while因为您可能会得到长期持有该属性的对。如果数字是错误的方式,只需交换它们。

在极少数情况下,您的号码选择中的余数为零(9/3, 8/4, 8/2, 6/3, 6/2, 4/2,M/M并且N/1(我认为)是详尽的列表)。如果您想要一组更扩展的方程式,我会选择另一种方式并选择两个数字来相乘,然后将第一个与结果交换。

例如,给定两个数字a = 3b = 7,只需执行以下操作:

a = a * b;

并且您可以a = 21, b = 7保证给出一个积分乘数并提供方程"21 / 7"


所以这个(伪代码)是我要开始的:

# Get the random values.

op = random_from ("+-/*")
n1 = random_from ("123456789")
n2 = random_from ("123456789")

# For subtraction, make sure n1 >= n2.

if op = "-" and n1 < n2:
    tmp = n1;
    n1 = n2;
    n2 = tmp;

# For division, make sure n1 is integral multiplier of n2.

if op = "/":
    n1 = n1 * n2

# Return expression in ALL cases.

return n1 + op + n2
于 2012-06-15T07:45:57.953 回答
2

你的while陈述中有错误的条件,你应该有完全相反的情况。如果您的条件都不匹配,您也不会为结果赋值,因此result = firstNumber + operators + secondNumber;应该将该行移出条件语句(或仅用简单的返回替换):

...
        if ((operators.Equals("/")) && (enableDouble == false))
        {
            while (firstNumber % secondNumber != 0) // <-- change here
            {
                firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
            }
        }
        // second condition was repearing while condition and that was redundant.
        else if (operators.Equals("-")) 
        {
            while (firstNumber < secondNumber) // <-- change here
            {
                firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
            }
        }
        return firstNumber + operators + secondNumber;
于 2012-06-15T07:44:29.817 回答
2
while (firstNumber % secondNumber == 0)

应该

while (firstNumber % secondNumber != 0)

while只要您提供的条件是 ,循环就会继续执行true。由于您希望firstNumber % secondNumber 等于 0;您应该循环并生成新值,只要firstNumber % secondNumber != 0.

我相信减法循环也有同样的问题。

最后(与您的问题无关)您可以==用来比较 C# 中的字符串。

if ((operators == "/") && (enableDouble == false))
于 2012-06-15T07:45:05.863 回答
1

在这两种情况下,您的 while 条件都是错误的,您需要将其更改为

while (firstNumber % secondNumber != 0) 

while (firstNumber < secondNumber)
于 2012-06-15T07:49:18.017 回答
1

这是我从您的问题中理解的解决方案。

if ((operators.Equals("/")) && (enableDouble == false))
    {
        while (firstNumber % secondNumber != 0)
        {
            firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
        }

        result = firstNumber + operators + secondNumber;

   }
            else if (operators.Equals("-") && (firstNumber<secondNumber))
            {
                while (firstNumber < secondNumber)
                {
                    firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                    secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                }

                result = firstNumber + operators + secondNumber;
            }
于 2012-06-15T07:57:24.207 回答
-1
  1. 通过制作两个不同的测试项目来实施测试用例
  2. 使用包含这些测试用例的 MSVS 单元测试项目报告
  3. 报告测试覆盖率在第一种情况下考虑MCDC,在第二种情况下考虑整体覆盖率,并评论发现不同覆盖率指标的原因

对于下面的代码

    namespace Funny
    {
    class QuesionsAndAnswers
    {
        private double firstNumber;
        private double secondNumber;
        private double userAnswer;
        private double computerAnswer;

        private string operators;

        private bool answerCorrect;
        private bool enableDouble;

        private double[] listOfNumbers;
        private string[] listOfOperators;

        private Random randomizer;

        private static QuesionsAndAnswers qa;

        private QuesionsAndAnswers()
        {
            randomizer = new Random();

            listOfNumbers = new double[] { 1,2,3,4,5,6,7,8,9 };
            listOfOperators = new string[] { "+", "-", "*", "/" };
        }

        public static QuesionsAndAnswers getQuesionsandAnswersInstance()
        {
            if (qa == null)
                qa = new QuesionsAndAnswers();
             return qa;
        }

        public string generateQuestions()
        {
            string result = "";

            operators = listOfOperators[randomizer.Next(listOfOperators.Length)];
            firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
            secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];

            if ((operators.Equals("/")) && (enableDouble == false))
            {
                while (firstNumber % secondNumber == 0)
                {
                    firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                }
                result = firstNumber + operators + secondNumber;
            }
            else if (operators.Equals("-") && (firstNumber<secondNumber))
            {
                while (firstNumber > secondNumber)
                {
                    firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                    secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
                }
                result = firstNumber + operators + secondNumber;
            }
            return result;
        }

        public void setDoubleAnswers(bool check)
        {
            enableDouble = check;
        }
    }
    }
于 2017-12-13T14:11:07.707 回答