Modulus operator with random numbers












0















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










share|improve this question




















  • 1





    assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

    – Gonzalo.-
    Nov 15 '18 at 21:48











  • how would I assign it without generating another set of different numbers?

    – mosquird
    Nov 15 '18 at 21:51






  • 4





    just do the assignment only once

    – Jonathan
    Nov 15 '18 at 21:52
















0















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










share|improve this question




















  • 1





    assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

    – Gonzalo.-
    Nov 15 '18 at 21:48











  • how would I assign it without generating another set of different numbers?

    – mosquird
    Nov 15 '18 at 21:51






  • 4





    just do the assignment only once

    – Jonathan
    Nov 15 '18 at 21:52














0












0








0








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










share|improve this question
















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







c# random user-input modulus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:30









Lennart

6,157125266




6,157125266










asked Nov 15 '18 at 21:46









mosquirdmosquird

196




196








  • 1





    assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

    – Gonzalo.-
    Nov 15 '18 at 21:48











  • how would I assign it without generating another set of different numbers?

    – mosquird
    Nov 15 '18 at 21:51






  • 4





    just do the assignment only once

    – Jonathan
    Nov 15 '18 at 21:52














  • 1





    assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

    – Gonzalo.-
    Nov 15 '18 at 21:48











  • how would I assign it without generating another set of different numbers?

    – mosquird
    Nov 15 '18 at 21:51






  • 4





    just do the assignment only once

    – Jonathan
    Nov 15 '18 at 21:52








1




1





assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

– Gonzalo.-
Nov 15 '18 at 21:48





assign them to a new variable, so you can spit them out in the WriteLine method, and then reuse them for the calculation

– Gonzalo.-
Nov 15 '18 at 21:48













how would I assign it without generating another set of different numbers?

– mosquird
Nov 15 '18 at 21:51





how would I assign it without generating another set of different numbers?

– mosquird
Nov 15 '18 at 21:51




4




4





just do the assignment only once

– Jonathan
Nov 15 '18 at 21:52





just do the assignment only once

– Jonathan
Nov 15 '18 at 21:52












2 Answers
2






active

oldest

votes


















2














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}?");





share|improve this answer


























  • Just to be picky, those are properties and not variables. Why not just local variables?

    – stuartd
    Nov 15 '18 at 21:50






  • 1





    fair enough. Changed.

    – Jonathan
    Nov 15 '18 at 21:52











  • oh wow, that makes so much more sense! Thank you so much!

    – mosquird
    Nov 15 '18 at 21:57



















0














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





share|improve this answer





















  • 3





    Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

    – Damien_The_Unbeliever
    Nov 16 '18 at 10:18











  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

    – Kurt Van den Branden
    Nov 16 '18 at 12:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328345%2fmodulus-operator-with-random-numbers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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}?");





share|improve this answer


























  • Just to be picky, those are properties and not variables. Why not just local variables?

    – stuartd
    Nov 15 '18 at 21:50






  • 1





    fair enough. Changed.

    – Jonathan
    Nov 15 '18 at 21:52











  • oh wow, that makes so much more sense! Thank you so much!

    – mosquird
    Nov 15 '18 at 21:57
















2














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}?");





share|improve this answer


























  • Just to be picky, those are properties and not variables. Why not just local variables?

    – stuartd
    Nov 15 '18 at 21:50






  • 1





    fair enough. Changed.

    – Jonathan
    Nov 15 '18 at 21:52











  • oh wow, that makes so much more sense! Thank you so much!

    – mosquird
    Nov 15 '18 at 21:57














2












2








2







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}?");





share|improve this answer















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}?");






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 21:51

























answered Nov 15 '18 at 21:49









JonathanJonathan

2,86921630




2,86921630













  • Just to be picky, those are properties and not variables. Why not just local variables?

    – stuartd
    Nov 15 '18 at 21:50






  • 1





    fair enough. Changed.

    – Jonathan
    Nov 15 '18 at 21:52











  • oh wow, that makes so much more sense! Thank you so much!

    – mosquird
    Nov 15 '18 at 21:57



















  • Just to be picky, those are properties and not variables. Why not just local variables?

    – stuartd
    Nov 15 '18 at 21:50






  • 1





    fair enough. Changed.

    – Jonathan
    Nov 15 '18 at 21:52











  • oh wow, that makes so much more sense! Thank you so much!

    – mosquird
    Nov 15 '18 at 21:57

















Just to be picky, those are properties and not variables. Why not just local variables?

– stuartd
Nov 15 '18 at 21:50





Just to be picky, those are properties and not variables. Why not just local variables?

– stuartd
Nov 15 '18 at 21:50




1




1





fair enough. Changed.

– Jonathan
Nov 15 '18 at 21:52





fair enough. Changed.

– Jonathan
Nov 15 '18 at 21:52













oh wow, that makes so much more sense! Thank you so much!

– mosquird
Nov 15 '18 at 21:57





oh wow, that makes so much more sense! Thank you so much!

– mosquird
Nov 15 '18 at 21:57













0














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





share|improve this answer





















  • 3





    Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

    – Damien_The_Unbeliever
    Nov 16 '18 at 10:18











  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

    – Kurt Van den Branden
    Nov 16 '18 at 12:26
















0














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





share|improve this answer





















  • 3





    Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

    – Damien_The_Unbeliever
    Nov 16 '18 at 10:18











  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

    – Kurt Van den Branden
    Nov 16 '18 at 12:26














0












0








0







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 1:19

























answered Nov 16 '18 at 10:07









陳建富陳建富

12




12








  • 3





    Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

    – Damien_The_Unbeliever
    Nov 16 '18 at 10:18











  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

    – Kurt Van den Branden
    Nov 16 '18 at 12:26














  • 3





    Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

    – Damien_The_Unbeliever
    Nov 16 '18 at 10:18











  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

    – Kurt Van den Branden
    Nov 16 '18 at 12:26








3




3





Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

– Damien_The_Unbeliever
Nov 16 '18 at 10:18





Don't just say that you "feixd (sic)" something, explain what you've changed and why. Stack Overflow is meant to be a site for Questions and Answers which help people. Not a game of "Spot the difference".

– Damien_The_Unbeliever
Nov 16 '18 at 10:18













Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

– Kurt Van den Branden
Nov 16 '18 at 12:26





Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

– Kurt Van den Branden
Nov 16 '18 at 12:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328345%2fmodulus-operator-with-random-numbers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python