C#窗体中按下一个button,在plane中出现20个随机数,按下相应的按键会出现这些随机数的平均值,代码已有
在控制台已经做出来了,想做成窗体的形式,大概想做成这个样子。代码写好了,usingSystem;usingSystem.Collections.Generic;using...
在控制台已经做出来了,想做成窗体的形式,大概想做成这个样子。
代码写好了,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NumericalRecipes;
namespace TextNaN
{
class Program
{
private static Random randObj = new Random();
//Tests for existence of unwanted NaN values in a data set
static void Main(string[] args)
{
int nPoints = 20;
double magnitude = 10.0;
double[] ydata = new double[nPoints];
double[] xdata = new double[nPoints];
for (int i = 0; i < ydata.Length; i++)
{
ydata[i] = magnitude * randObj.NextDouble();
xdata[i] = ydata[i];
Console.WriteLine("{0}", Math.Round(ydata[i], 2));
}
double[] weights = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0,
6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
16.0, 17.0, 18.0, 19.0, 20.0
};
Console.WriteLine("Mean = {0}", NumericalRecipes.mean.Mean(ydata));
Console.WriteLine("MinValue = {0}", NumericalRecipes.Min_Value.MinValue(ydata));
Console.WriteLine("MinAbsValue = {0}", NumericalRecipes.Min_AbsValue.MinAbsValue(ydata));
Console.WriteLine("MaxValue = {0}", NumericalRecipes.Max_Value.MaxValue(ydata));
Console.WriteLine("MaxIndex = {0}", NumericalRecipes.Max_Index.MaxIndex(ydata));
Console.WriteLine("Count = {0}", NumericalRecipes.count.Count(ydata));
Console.WriteLine("Sum = {0}", NumericalRecipes.sum.Sum(ydata));
Console.ReadLine();
}
}
}
NumericalRecipes;这个类库是自己编的, 展开
代码写好了,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NumericalRecipes;
namespace TextNaN
{
class Program
{
private static Random randObj = new Random();
//Tests for existence of unwanted NaN values in a data set
static void Main(string[] args)
{
int nPoints = 20;
double magnitude = 10.0;
double[] ydata = new double[nPoints];
double[] xdata = new double[nPoints];
for (int i = 0; i < ydata.Length; i++)
{
ydata[i] = magnitude * randObj.NextDouble();
xdata[i] = ydata[i];
Console.WriteLine("{0}", Math.Round(ydata[i], 2));
}
double[] weights = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0,
6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
16.0, 17.0, 18.0, 19.0, 20.0
};
Console.WriteLine("Mean = {0}", NumericalRecipes.mean.Mean(ydata));
Console.WriteLine("MinValue = {0}", NumericalRecipes.Min_Value.MinValue(ydata));
Console.WriteLine("MinAbsValue = {0}", NumericalRecipes.Min_AbsValue.MinAbsValue(ydata));
Console.WriteLine("MaxValue = {0}", NumericalRecipes.Max_Value.MaxValue(ydata));
Console.WriteLine("MaxIndex = {0}", NumericalRecipes.Max_Index.MaxIndex(ydata));
Console.WriteLine("Count = {0}", NumericalRecipes.count.Count(ydata));
Console.WriteLine("Sum = {0}", NumericalRecipes.sum.Sum(ydata));
Console.ReadLine();
}
}
}
NumericalRecipes;这个类库是自己编的, 展开
展开全部
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.Threading;
namespace Demo
{
public partial class Form1 : Form
{
private int nPoints = 20;
private double magnitude = 10.0;
private double[] ydata;
private double[] xdata;
public Form1()
{
InitializeComponent();
textBox1.Multiline = true;//允许多行
ydata = new double[nPoints];
xdata = new double[nPoints];
}
//按钮事件
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
double maxnum = -1;
double all = 0;
for (int i = 0; i < ydata.Length; i++)
{
Random r = new Random();
ydata[i] = r.NextDouble() * magnitude;
xdata[i] = ydata[i];
if (ydata[i] > maxnum)
{
maxnum = ydata[i];
}
all = all + ydata[i];
textBox1.Text = textBox1.Text + ydata[i].ToString("0.00") +"\r\n";
Thread.Sleep(10);//延时 保证取到随机数有随机
}
textBox2.Text = maxnum.ToString("0.00");
textBox3.Text = (all / ydata.Length).ToString("0.00");
}
}
}
注意:看到是毕业设计 Random是伪随机 如果随机数要求比较高的话可以参考:
http://www.cnblogs.com/izanami/archive/2011/04/20/2022173.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询