如何生成随机数学运算 [英] How to generate random math operation
问题描述
我的代码有什么问题?我想创建一个函数,它将调用一个随机数和多个或者除以(以随机顺序)。当我启动应用程序时,乘法(或除法)的总结果有时是正确的,但有时只等于0.
我应该如何解决问题?
我尝试过:
随机rand = new Random();
int num1 = rand.Next(1,15);
int num2 = rand.Next(1,15);
int RandOperation = rand.Next(1,100);
if(RandOperation%2 == 0)
{
if(num2%num1 == 0& num2> num1)
{
Display.Text = Convert.ToString(num2)+/+ Convert.ToString(num1);
total = num2 / num1;
}
else
{
if(num1%num2 == 0& num1> num2)
{
Display.Text = Convert.ToString(num1)+/+ Convert.ToString(num2);
total = num1 / num2;
}
}
}
其他
{
Display.Text = Convert.ToString(num1)+*+ Convert.ToString(num2) ;
total = num1 * num2;
}
嗯......如果num1是11且num2是14,那么会发生什么?RandOperation是74?
它通过了这个测试:
if (RandOperation% 2 == 0 )
但是它没有通过这个测试:
if (num2%num1 == 0 & num2 > num1)
它没有通过这个测试:
if (num1%num2 == 0 & num1 > num2)
所以..根本不对总计
做任何事情,你得到零。
试一试:使用调试器并按照执行通过 - 你会明白我的意思!
< b>好的我会尝试,但也许有一种简单的方法可以产生我想要的东西?
嗯......这将取决于正是你想要的! :笑:
我不确定你为什么要在if条件下使用余数 - 我这样做的方式有点简单:
< pre lang =c#>随机rand = new Random();
int num1 = rand.Next( 1 , 15 跨度>);
int num2 = rand.Next( 1 , 15 跨度>);
int RandOperation = rand.Next( 0 , 2 跨度>);
if (RandOperation == 0 )
{
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
Display.Text = string .Format( {0} / {1},num1,num2);
total = num1 / num2;
}
else
{
Display.Text = string .Format( {0} * {1},num1,num2);
total = num1 * num2;
}
What's wrong in my code? I want to create function which will call a random numbers and multiple that or divide(in the random order). When I start the app, total result of multiplication(or division) is sometimes correct, but sometimes just equal 0.
How should I solve the problem?
What I have tried:
Random rand = new Random(); int num1 = rand.Next(1,15); int num2 = rand.Next(1,15); int RandOperation = rand.Next(1,100); if (RandOperation % 2 == 0) { if (num2 % num1 == 0 & num2 > num1) { Display.Text = Convert.ToString(num2) + "/" + Convert.ToString(num1); total = num2 / num1; } else { if (num1 % num2 == 0 & num1 > num2) { Display.Text = Convert.ToString(num1) + "/" + Convert.ToString(num2); total = num1 / num2; } } } else { Display.Text = Convert.ToString(num1) + "*" + Convert.ToString(num2); total = num1 * num2; }
Well...What happens if num1 is 11 and num2 is 14, and RandOperation is 74?
It passes this test:
if (RandOperation % 2 == 0)
But it fails this test:
if (num2 % num1 == 0 & num2 > num1)
And it fails this test:
if (num1 % num2 == 0 & num1 > num2)
So...it doesn't do anything tototal
at all, and you get a zero.
Try it: use the debugger and follow the execution through - you'll see what I mean!
"Ok I will try, but maybe there is a simple way to generate what I want?"
Well...that's going to depend on exactly what you want! :laugh:
I'm not sure why you are using the remainder in the if conditions at all - the way I'd do it is a bit simpler:
Random rand = new Random(); int num1 = rand.Next(1, 15); int num2 = rand.Next(1, 15); int RandOperation = rand.Next(0, 2); if (RandOperation == 0) { if (num1 < num2) { int temp = num1; num1 = num2; num2 = temp; } Display.Text = string.Format("{0} / {1}", num1, num2); total = num1 / num2; } else { Display.Text = string.Format("{0} * {1}", num1, num2); total = num1 * num2; }
这篇关于如何生成随机数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!