user10602246
user10602246

Reputation:

Modulus operator with random numbers

What I want to do is have random numbers be generated and take those random numbers and put them through a modulus operator. And I want it to ask the user for the answer they think it is and then they will be told if it is right or wrong. This is what I have.

Random rand = new Random();
int minA;
int maxA;
int minB;
int maxB;
int usersAnswer;

Console.WriteLine("what is the minimum value: ");
Int32.TryParse(Console.WriteLine(), out minA);

Console.WriteLine("what is the minimum value: ");
Int32.TryParse(Console.WriteLine(), out maxA);

Console.WriteLine("what is the minimum value: ");
Int32.TryParse(Console.WriteLine(), out minB);

Console.WriteLine("what is the minimum value: ");
Int32.TryParse(Console.WriteLine(), out maxB);

Console.WriteLine("What is the result of {0} % {1}? ", rand.Next(minA, maxA), rand.Next(minB, maxB)); 
Int32.TryParse(Console.ReadLine(), out usersAnswer);
answer = //directly implementing the random numbers generated with modulous operator)
if(userAnswer == answer)
{
    Console.WriteLine("{0} is correct", answer);
}
else
{
    Console.WriteLine("Good try, but no: {the random number} % {the other random number} = {0}", not sure, not sure, answer)
}    

So what I want to know is how I can directly implement the random numbers already generated from "Console.WriteLine("What is the result of {0} % {1}? ", rand.Next(minA, maxA), rand.Next(minB, maxB));" into a modulus operator equation and get the answer. I hope this all made sense

Upvotes: 0

Views: 1099

Answers (2)

陳建富
陳建富

Reputation: 1

Your code have some promblem:

  1. Remember to fix your instructions text
  2. Somthimes, you use writeline(),but readline() actually
  3. You should handle that something might be going wrong, check comment out

Try it

    static void Main(string[] args)
    {
        Random rand = new Random();
        int minA, maxA;
        int minB, maxB;
        int userAnswer;

        Console.WriteLine("what is the minimum A: ");
        if (!Int32.TryParse(Console.ReadLine(), out minA)) { return; } //If something going wrong, you should handle it.

        Console.WriteLine("what is the maximum A: ");
        if (!Int32.TryParse(Console.ReadLine(), out maxA)) { return; }

        Console.WriteLine("what is the minimum B: ");
        if (!Int32.TryParse(Console.ReadLine(), out minB)) { return; }

        Console.WriteLine("what is the maximum B: ");
        if (!Int32.TryParse(Console.ReadLine(), out maxB)) { return; }

        if (minA > maxA) { exchange(ref minA, ref maxA); } //User might have typo,and this is one way to fix it.
        if (minB > maxB) { exchange(ref minB, ref maxB); }

        int rndA = rand.Next(minA, maxA),
            rndB = rand.Next(minB, maxB); //You should restore the random result, or lost it
        int result;
        try
        {
            result = calcMod(rndA, rndB); //Directly implementing the random numbers generated with modulous operator
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            return;
        }

        Console.WriteLine($"What is the result of {rndA} % {rndB}? ");
        Int32.TryParse(Console.ReadLine(), out userAnswer);
        if (userAnswer == result)
        {
            Console.WriteLine("{0} is correct", result);
        }
        else
        {
            Console.WriteLine($"Good try, but no: {rndA} % {rndB} = {result}");
        }

        Console.Write("\nPress Any key to leave.");
        Console.ReadKey();
    }

    //Calculate mod result
    static int calcMod(int i1, int i2)
    {
        try
        {
            return i1 % i2;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    //Swap number
    static void exchange(ref int i1, ref int i2)
    {
        int tmp;

        tmp = i1;
        i1 = i2;
        i2 = tmp;
    }

Upvotes: 0

Jonathan
Jonathan

Reputation: 5018

You should store your 2 random numbers as new variables in your class:

 int RandomOne;
 int RandomTwo;

assign them further down

 RandomOne = rand.Next(minA, maxA);
 RandomTwo = rand.Next(minA, maxA);

and then refer to them in your messaging. Something like:

 Console.WriteLine($"What is the result of {RandomOne} % {RandomTwo}?");

Upvotes: 2

Related Questions