关于随机函数的问题
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1;
//using System.Timers;
namespace ConsoleApplication1
{
struct role
{
public int HP;
public int Attact;
public string name;
public Sex Sex1;
}
enum Sex
{
woman,
man
}
class Program
{
static role CreateRole(role r1)
{
r1.name = Convert.ToString(Console.ReadLine());
Random ran = new Random();
r1.HP = ran.Next(400, 700);
Console.WriteLine("请输入您的性别(man or woman):");
string sex;
sex = Convert.ToString(Console.ReadLine());
r1.Sex1 = (Sex)Enum.Parse(typeof(Sex), sex);
return r1;
}
static int attact(role r2)
{
Random ran = new Random(unchecked((int)DateTime.Now.Ticks));
r2.Attact = ran.Next(30, 70);
return r2.Attact;
}
static void Main(string[] args)
{
int count=2;
int i;
//count = Convert.ToInt16(Console.ReadLine());
role[] r;
r = new role[count];
Console.WriteLine("您共创建了{0}个角色", count);
for(i=0;i<count;i++)
{
Console.WriteLine("请输入第{0}个角色的姓名", i + 1);
r[i]=CreateRole(r[i]);
Console.WriteLine("{1}的血量是{0},性别{2}", r[i].HP, r[i].name,r[i].Sex1);
r[i].Attact = attact(r[i]);
Console.WriteLine("基本攻击力是{0}~{1}",attact(r[i])-13,attact(r[i])+10);
}
Console.WriteLine("按任意键比赛开始");
Console.ReadLine();
int j=0;
int miss;
while(r[0].HP>0&&r[1].HP>0)
{
//System.Timers.Timer time = new System.Timers.Timer(1000);
//time.Elapsed+=new System.Timers.ElapsedEventHandler(fighting);
Random rand = new Random();
if (j%2 == 0)
{
miss = rand.Next(1,5);
if (miss == 1)
{
r[0].Attact=attact(r[0]);
r[1].HP = r[1].HP - r[0].Attact;
Console.WriteLine("{0}攻击了{1},照成{2}点伤害", r[0].name, r[1].name, r[0].Attact);
Console.WriteLine("{0},{1}", miss, r[0].Attact);
j++;
}
else
{
r[0].Attact=2*attact(r[0]);
r[1].HP = r[1].HP - r[0].Attact;
Console.WriteLine("{0}爆发了,照成{1}{2}点伤害", r[0].name, r[1].name, r[0].Attact);
Console.WriteLine("{0},{1}", miss, r[0].Attact);
j++;
}
}
else if (j%2 == 1)
{
miss=rand.Next(1, 2);
if (miss == 1)
{
r[1].Attact=attact(r[1]);
r[0].HP = r[0].HP - r[1].Attact;
Console.WriteLine("{0}攻击了{1},照成{2}点伤害", r[1].name, r[0].name, r[1].Attact);
Console.WriteLine("{0},{1}", miss, r[1].Attact);
j--;
}
else
{
r[1].Attact=2*attact(r[1]);
r[0].HP = r[0].HP - r[1].Attact;
Console.WriteLine("{0}爆发了,照成{1}{2}点伤害", r[1].name, r[0].name, r[1].Attact);
Console.WriteLine("{0},{1}", miss, r[1].Attact);
j--;
}
}
if (r[0].HP<0)
{
Console.WriteLine("{0}赢了",r[0].name);
}
else if (r[1].HP<0)
{
Console.WriteLine("{0}赢了", r[1].name);
}
}
Console.WriteLine("按任意键结束");
Console.ReadLine();
}
}
}
为什么我运行后,并没有实现攻击力的随机化?谢谢各位哈