C# 随机数模算子

C# 随机数模算子,c#,random,user-input,modulus,C#,Random,User Input,Modulus,我想做的是生成随机数,然后取这些随机数,并将它们通过模运算符。我想让它问用户他们认为答案是什么,然后告诉他们答案是对的还是错的。这就是我所拥有的 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 m

我想做的是生成随机数,然后取这些随机数,并将它们通过模运算符。我想让它问用户他们认为答案是什么,然后告诉他们答案是对的还是错的。这就是我所拥有的

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)
}    
所以我想知道的是如何将已经从“Console.WriteLine”(“{0}%{1}的结果是什么?”,rand.Next(minA,maxA),rand.Next(minB,maxB));”生成的随机数直接实现到模算子方程中,并得到答案。
我希望这一切都是有意义的

你应该将你的2个随机数作为新变量存储在课堂上:

 int RandomOne;
 int RandomTwo;
再往下分配

 RandomOne = rand.Next(minA, maxA);
 RandomTwo = rand.Next(minA, maxA);
然后在您的消息中引用它们。比如:

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

您应该将2个随机数作为新变量存储在类中:

 int RandomOne;
 int RandomTwo;
再往下分配

 RandomOne = rand.Next(minA, maxA);
 RandomTwo = rand.Next(minA, maxA);
然后在您的消息中引用它们。比如:

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

您的代码有一些问题:

  • 请记住修改您的说明文本
  • 有时,您使用writeline(),但实际上使用readline()
  • 您应该处理可能出现的问题,查看评论
  • 试试看

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

    您的代码有一些问题:

  • 请记住修改您的说明文本
  • 有时,您使用writeline(),但实际上使用readline()
  • 您应该处理可能出现的问题,查看评论
  • 试试看

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

    将它们分配给一个新变量,这样你就可以在WriteLine方法中吐出它们,然后再将它们用于计算我如何分配它而不生成另一组不同的数字?只需将它们分配给一个新变量,这样你就可以在WriteLine方法中吐出它们,然后在计算中重复使用它们我如何分配它而不生成另一组不同的数字?只要只分配一次就行了为了挑剔,这些是属性而不是变量。为什么不只是局部变量?这很公平。更改。只是为了挑剔,这些是属性而不是变量。为什么不只是局部变量?这很公平。改变。不要只是说你“feixd(原文如此)”什么,解释一下你改变了什么以及为什么。Stack Overflow是一个提供帮助人们的问题和答案的网站。不是一个“发现差异”的游戏。欢迎来到Stack Overflow!虽然这段代码片段可能是解决方案,但包含解释确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。不要只说您“feixd(sic)”什么,请解释您更改了什么以及为什么。Stack Overflow是一个提供帮助人们的问题和答案的网站。不是一个“发现差异”的游戏。欢迎来到Stack Overflow!虽然这段代码片段可能是解决方案,但包含解释确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。