c# - 从随机数中取出另一个数字的百分比 [英] c# - making a percent from a random number out of another number
本文介绍了c# - 从随机数中取出另一个数字的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧所以这是一个游戏命令,它运作完美,但我无法弄清楚如何使十进制pc = num * rand / 100,因为兰德并没有真正定义一个数字。
内部 class GambleCommand:命令
{
public GambleCommand()
: base ( roll, 3 )
{
}
protected override bool 进程(播放器播放器,RealmTime时间,字符串 [] args)
{
尝试
{
if (args.Length == 1 )
{
int num = int .Parse(args [ 0 ]);
Random rnd = new Random();
string pl = player.Name;
string rand = ;
if (num < 0 )rand = rnd.Next(num, 0 )。ToString();
else rand = rnd.Next( 0 ,num).ToString();
decimal pc = num * rand / 100 ;
foreach (客户i player.Manager.Clients.Values)
{
i.SendPacket( new TextPacket
{
BubbleTime = 0 ,
Stars = -1,
Name = pl,
Text = roll + rand + out of + num + 。( + pc + %)
});
}
}
}
catch
{
Random rnd = new Random();
string pl = player.Name;
string rand = rnd.Next( 0 , 100 跨度>)的ToString();
foreach (客户i player.Manager.Clients.Values)
{
i.SendPacket( new TextPacket
{
BubbleTime = 0 ,
Stars = -1,
Name = pl,
Text = roll a + rand + 满分100分。
});
}
}
返回 true ;
}
}
除了那个命令基本上从0-中选出一个数字之外别无其他解释num(num是您选择的数字)。我想要它,以便它在游戏中说出它的百分比。请尽快帮助!
解决方案
首先,您应该尝试使用整数
int rand;
...
rand = rnd.Next(num, 0 );
并更改公式
int pc = rand * 100 / NUM;
应该更好!
也许尝试小数。 Parse或decimal.TryParse,如果你想继续进行字符串转换。
decimal pc = num * decimal .Parse(rand)/ 100 ;
好吧我修好了所以我不得不把它更改为
decimal pc = decimal .Parse(rand)/ num * 100 ;
Alright so this is an in game command and it works perfect but I just can't figure out how to make decimal pc = num * rand / 100 because rand doesn't really define one number.
internal class GambleCommand : Command
{
public GambleCommand()
: base("roll", 3)
{
}
protected override bool Process(Player player, RealmTime time, string[] args)
{
try
{
if (args.Length == 1)
{
int num = int.Parse(args[0]);
Random rnd = new Random();
string pl = player.Name;
string rand = "";
if (num < 0) rand = rnd.Next(num, 0).ToString();
else rand = rnd.Next(0, num).ToString();
decimal pc = num * rand / 100;
foreach (Client i in player.Manager.Clients.Values)
{
i.SendPacket(new TextPacket
{
BubbleTime = 0,
Stars = -1,
Name = pl,
Text = "rolled a " + rand + " out of " + num + ". (" + pc + "%)"
});
}
}
}
catch
{
Random rnd = new Random();
string pl = player.Name;
string rand = rnd.Next(0, 100).ToString();
foreach (Client i in player.Manager.Clients.Values)
{
i.SendPacket(new TextPacket
{
BubbleTime = 0,
Stars = -1,
Name = pl,
Text = "rolled a " + rand + " out of 100."
});
}
}
return true;
}
}
There isn't to much to explain other then that the command basically picks a number out of 0-num (num is a number that you choose). I want it so that it gives the percent when it says it in game. Please help asap!
解决方案
First, you should try with integer
int rand; ... rand = rnd.Next(num, 0);
And change the formula
int pc = rand * 100 / NUM;
Should be better !
Maybe try decimal.Parse or decimal.TryParse, if you want to continue on the road of string conversions.
decimal pc = num * decimal.Parse(rand) / 100;
Alright I fixed it so I had to change it to
decimal pc = decimal.Parse(rand) / num * 100;
这篇关于c# - 从随机数中取出另一个数字的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文