簡體   English   中英

生成帶有隨機數的列表。 排序和總和

[英]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 instanceone 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM