![](/img/trans.png)
[英]Generate a list of (count) Random Numbers that total to a specified average value of (avg)
[英]Generate list with random numbers. Sort and total sum
我是编程新手,如果有人可以帮助我解决以下问题,我将非常高兴。 我有两种方法的问题。 首先将列表中的所有50个随机数相加,然后将50个随机数与第二个排序列表相加。 我知道当列表有四个数字{1,2,3,8}却没有50个随机数时该怎么做。
最好为这两个方法使用构造函数,但是现在我不知道如何做到这一点。
任何帮助将非常感激。
class Class1
{
var rand = new Random();
public Class1( ) // how to use a constructor ?
public static List<double> TotalSum()
{
var alist = new List<double>();
for (int i = 0; i < 50; i++)
{
alist.Add(rand.Next(10));
}
Console.WriteLine("Total sum:", alist);
}
public static List<double> Sort()
{
var slist = new List<double>();
for (int i = 0; i < 50; i++)
{
slist.Sort(rand.Next(10)); // rand.Next does not work with Sort
Console.WriteLine(slist);
}
}
class Program
{
static void Main(string[] args)
{
Class1 show = new Class1();
show.TotalSum();
show.Sort();
}
}
}
只需使用2种方法
list.Sum();
list.Sort();
我建议您使用rand.Next(Environment.TickCount)获得更好的随机数,但是最后您需要使用其他原始值来存储结果
class Program
{
static void Main(string[] args)
{
var array = Class1.Sort();
PrintArray(array);
Console.WriteLine();
Console.WriteLine("Total sum: {0}", Class1.TotalSum());
Console.ReadLine();
}
static void PrintArray(List<double> array)
{
foreach (var a in array)
Console.WriteLine(a);
}
}
public class Class1
{
public static double TotalSum()
{
var rand = new Random();
var alist = new List<double>();
for (int i = 0; i < 50; i++)
{
alist.Add(rand.Next(10));
}
return alist.Sum();
}
public static List<double> Sort()
{
var rand = new Random();
var slist = new List<double>();
for (int i = 0; i < 50; i++)
{
slist.Add(rand.Next(10)); // rand.Next does not work with Sort
}
slist.Sort();
return slist;
}
}
我想我明白你的意思。 您需要一个包含随机数字列表的类,并且需要一些函数。
最好给您的班级起一个更合适的名字。 例如“ RandomList”会很好。
您不能使用Console.WriteLine(list)
打印列表项。 您必须遍历所有项目并以所需方式打印它们。
您不能使用var
来定义字段。 您必须显式地编写类型。
换句话说, static
关键字one for all instances
。 因此,如果您不使用静态,则one per instance
为one per instance
。 实例只是您使用构造函数创建的对象。 最好将随机数生成器声明为静态,但其他方法和列表字段应保持非静态。
使用Linq Sum()
您可以从列表中获取所有项目的总和。 如果您不想使用Linq,则必须遍历list并将每个项目添加到value sum
。
我注释了代码以解释事情。
class Program
{
static void Main(string[] args)
{
RandomList show = new RandomList(50, 10);
show.TotalSum();
show.Sort();
}
}
class RandomList
{
private static Random rand = new Random();
private List<double> _list; // this field holds your list
public RandomList(int length , int maxValuePerItem) // this is the constructor
{
_list = new List<double>();
// populate list
for (int i = 0; i < length; i++)
{
_list.Add(rand.Next(maxValuePerItem));
}
}
public void TotalSum()
{
Console.WriteLine("Total sum: {0}", _list.Sum()); // {0} is required to specify the place to put _list.Sum() inside string.
// without linq way
// int sum = 0;
// foreach(var i in _list) sum += i;
}
public void Sort()
{
_list.Sort();
foreach (var d in _list)
{
Console.Write(d + " , "); // you need to print this in the way you want.
}
}
}
通常使用构造函数来初始化一些值。 在您的情况下,应使用它来初始化列表,即为它加载50个随机数。 我们还将对其进行更改,以使列表成为该类的属性。
请注意,方法不应是静态的,因为它们作用于类的属性。 我们还使用List
的内置方法( Sum()
和Sort()
),而不是滚动自己的方法。
class Class1
{
var rand = new Random();
var alist;
public Class1() // constructor creates a new list and initializes it
{
alist = new List<double>();
for (int i = 0; i < 50; i++)
{
alist.Add(rand.Next(10));
}
}
public List<double> TotalSum()
{
Console.WriteLine("Total sum:", alist.Sum());
}
public List<double> Sort()
{
alist.Sort();
for (double num in alist)
{
Console.WriteLine(num.ToString());
}
}
class Program
{
static void Main(string[] args)
{
Class1 show = new Class1();
show.TotalSum();
show.Sort();
}
}
}
因为您询问了如何使用构造函数,所以我试图以构造函数执行一些有用的方式来重写代码。 我添加了一个属性和一个局部变量以及另一个函数。
public class Class1
{
Random rand = new Random();
List<double> alist { get; set; }
public Class1(int howmany = 50)
{
alist = new List<double>();
for (var i = 0; i < howmany; i++)
{
alist.Add(rand.NextDouble());
}
}
public void Sort()
{
alist.Sort();
}
public void printTotalSum()
{
Console.WriteLine("Total sum: {0}", alist.Sum());
}
public void printList() {
Console.WriteLine("The list contains:");
for (int i = 0; i < alist.Count; i++)
{
Console.WriteLine(alist[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
Class1 show = new Class1(10);
show.printTotalSum();
show.Sort();
show.printList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.