| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 285 人关注过本帖
标题:关于随机函数的问题
只看楼主 加入收藏
devil_sex
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-2
收藏
 问题点数:0 回复次数:0 
关于随机函数的问题
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();
               
        }
    }
}
为什么我运行后,并没有实现攻击力的随机化?谢谢各位哈
搜索更多相关主题的帖子: 随机函数 
2010-04-07 13:18
快速回复:关于随机函数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044389 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved