自己开始尝试弄一下随机数,照着方法,自己做了个英雄打怪兽的测试
int heroAttack; int monsterDefend = 10; int monsterLife = 10; Random attack = new Random(); //初始化一个随机数的类 heroAttack = attack.Next(8, 26); //让随机数范围在8到25之间,注意是25,不是26 Console.WriteLine("你的攻击力为" + heroAttack); if (heroAttack - monsterDefend > 0 && monsterLife + monsterDefend - heroAttack > 0) { monsterLife = 10 - heroAttack + monsterDefend; Console.WriteLine("怪物被打掉{0}血,怪物剩余{1}血",heroAttack-monsterDefend,monsterLife); } else if (monsterLife + monsterDefend - heroAttack <= 0) { Console.WriteLine("恭喜你,一拳打死怪物"); } else { Console.WriteLine("抱歉,你未对怪物造成伤害,太弱了"); } Console.ReadLine();
其实主要就是 Random attack = new Random();
heroAttack = attack.Next(8, 26);
一个生成了骰子类,一个使用了骰子,并交代了范围