如何在 C# 中将 int Random 乘以 int
How to multiply int Random by int in C#
我遇到了一个小问题 - 我正在尝试在控制台应用程序中将 random 乘以 int。我有一个错误:
Operator '*' cannot be applied to operands of Type 'Random' and 'int'
我骑过:rndAttackPoints = attackPoints
然后
这个int damagePoints = (rndAttackPoints * strenghtPoints1) - defencePoints1;
变成
这个 int damagePoints = (attackPoints * strenghtPoints1) - defencePoints1;
但这没有用。
所以我问你,你知道如何在 C# 中将 Random
乘以 int
吗?
//Elf
int healthPoints1 = 100;
int defencePoints1 = 3;
int strenghtPoints1 = 2;
// Goblin
int healthPoints2 = 100;
int defencePoints2 = 5;
int strenghtPoints2 = 1;
Random rndAttackPoints = new Random();
Console.WriteLine($"You've attacked: {rndAttackPoints.Next(5, 10)} HP");
int damagePoints = (rndAttackPoints * strenghtPoints1) - defencePoints1;
将随机值保存在一个单独的变量中并按如下方式使用它:
Random rndAttackPoints = new Random();
int rnd = rndAttackPoints.Next(5, 10);
Console.WriteLine($"You've attacked: {rnd} HP");
int damagePoints = (rnd * strenghtPoints1) - defencePoints1;
我遇到了一个小问题 - 我正在尝试在控制台应用程序中将 random 乘以 int。我有一个错误:
Operator '*' cannot be applied to operands of Type 'Random' and 'int'
我骑过:rndAttackPoints = attackPoints
然后
这个int damagePoints = (rndAttackPoints * strenghtPoints1) - defencePoints1;
变成
这个 int damagePoints = (attackPoints * strenghtPoints1) - defencePoints1;
但这没有用。
所以我问你,你知道如何在 C# 中将 Random
乘以 int
吗?
//Elf
int healthPoints1 = 100;
int defencePoints1 = 3;
int strenghtPoints1 = 2;
// Goblin
int healthPoints2 = 100;
int defencePoints2 = 5;
int strenghtPoints2 = 1;
Random rndAttackPoints = new Random();
Console.WriteLine($"You've attacked: {rndAttackPoints.Next(5, 10)} HP");
int damagePoints = (rndAttackPoints * strenghtPoints1) - defencePoints1;
将随机值保存在一个单独的变量中并按如下方式使用它:
Random rndAttackPoints = new Random();
int rnd = rndAttackPoints.Next(5, 10);
Console.WriteLine($"You've attacked: {rnd} HP");
int damagePoints = (rnd * strenghtPoints1) - defencePoints1;