具有随机数的模运算符

时间:2018-11-15 21:46:28

标签: 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 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)
}    

所以我想知道如何直接实现已经从“ C​​onsole.WriteLine(” {0}%{1}的结果是什么?“,rand.Next(minA,maxA)生成的随机数。 ,rand.Next(minB,maxB));“进入模运算符方程并得到答案。 我希望一切都有意义

2 个答案:

答案 0 :(得分:2)

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

 int RandomOne;
 int RandomTwo;

将它们分配得更远

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

,然后在您的消息中引用它们。像这样:

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

答案 1 :(得分:0)

您的代码有问题:

  1. 记住要修正您的说明文字
  2. Somthimes,您使用writeline(),但实际上使用readline()
  3. 您应该处理可能出现问题的地方,请注释掉

尝试

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