PeakGen
PeakGen

Reputation: 23015

if else statement and while loop

Please have a look at the following code

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;
        }
    }
}

In here, in the generateQuestions() method, I am trying to generate some math questions.

If operator in "-" and if the firstNumber < secondNumber, then should re generate numbers (first number and second number) to make sure the answer is not a minus value.

And, if the operator is "/" and the boolean value is false, it should regenerate numbers again to make sure the the answer (which means the calculation, for an example 2/1 = 2) doesn't contain any floating points (which means it should not generate questions like 2/3, because the answer contains floating points).

In my attempt, both above are not happening. It still generates unexpected answers or, sometimes nothing. Why is that? Please help

Please note, I am a Java developer and this is my first major C# project.

Upvotes: 2

Views: 26800

Answers (6)

khalid khan
khalid khan

Reputation: 1

  1. Implement test cases by making two different test projects
  2. Report using MSVS unit test project containing these test cases
  3. Report Test coverage considering MCDC in first case and overall coverage in the second case and comment on the reasons why different coverage metric was found

For the Below Code

    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;
        }
    }
    }

Upvotes: -1

paxdiablo
paxdiablo

Reputation: 881263

Well, it returns nothing sometimes because, other than the edge cases you're catching, you never set result to anything other than "".

And both your while statements are the wrong way around. It should be:

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

However, for efficiency, I wouldn't do that second while since you may get a long run of pairs where that property holds. If the numbers are the wrong way around, just swap them.

And there's precious few cases where the remainder will be zero from your number selection (9/3, 8/4, 8/2, 6/3, 6/2, 4/2, M/M and N/1 is (I think) the exhaustive list). If you want a more expanded set of equations, I would go the other way and choose two numbers to mutiply, then swap the first with the result.

For example, given the two numbers a = 3 and b = 7, simply do:

a = a * b;

and you have a = 21, b = 7 which is guaranteed to give an integral multiplier and deliver the equation "21 / 7".


So this (psuedo-code) is what I would start with:

# 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

Upvotes: 6

Estefany Velez
Estefany Velez

Reputation: 328

Here is the solution to what I understand from your question.

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;
            }

Upvotes: 1

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

You have wrong conditions in your while statements, you should have exactly the opposite. Also you're not assigning value to result if none of your conditions matches, so the line result = firstNumber + operators + secondNumber; should be moved out of conditional statements (or just replaced with simple return):

...
        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;

Upvotes: 2

Bob Vale
Bob Vale

Reputation: 18474

In both cases your while conditions are the wrong way round, you need to change it to

while (firstNumber % secondNumber != 0) 

and

while (firstNumber < secondNumber)

Upvotes: 1

Rob P.
Rob P.

Reputation: 15071

while (firstNumber % secondNumber == 0)

Should be

while (firstNumber % secondNumber != 0)

The while loop will continue to execute so long as the condition you provide is true. Since you want the firstNumber % secondNumber to equal 0; you should loop and generate the new value so long as firstNumber % secondNumber != 0.

I believe you have the same problem with the subtraction loop as well.

Finally (unrelated to your question) you can use == to compare strings in C#.

if ((operators == "/") && (enableDouble == false))

Upvotes: 2

Related Questions