首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#下机 产生随机数

2012-11-23 
C#上机产生随机数/* * 程序头部注释开始* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机

C#上机 产生随机数

/* * 程序头部注释开始   * 程序的版权和版本声明部分   * Copyright (c) 2011, 烟台大学计算机学院学生   * All rights reserved.   * 文件名称:产生随机数                         * 作    者:薛广晨                               * 完成日期:2012  年 11 月  19  日   * 版 本号:x1.0               * 对任务及求解方法的描述部分   * 输入描述:  * 问题描述: 利用Random类产生10个[10,99]之间的随机数,并将这10个随机数在列表框中显示出来,* 每个数占一项。用户选择某项后,在右边标签中显示所选内容* 程序输出:   * 程序头部的注释结束 */using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Collections;namespace WindowsFormsApplication6{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            Hashtable hashtable = new Hashtable(); //产生不重复的随机数            Random random = new Random();            for(int i=0; i< 10; i++)            {                int value = random.Next(10, 100);                if (!hashtable.ContainsValue(value) && value != 0)                  {                      hashtable.Add(value, value);                      listBox1.Items.Add(value.ToString());                  }              }        }        private void label1_Click(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            this.Close();        }        private void listBox1_SelectedValueChanged(object sender, EventArgs e)        {            label1.Text = listBox1.Text;        }    }}


运行结果:

C#下机  产生随机数

热点排行