获取随机列表输出

时间:2019-06-09 15:13:23

标签: c#

我有下面的代码,我需要读取文本文件的一行,并从文本文件的diffendend行中以随机顺序写出这一行。 我怎么做?

我已经尝试过使用Next(),但是似乎没有捕获文本文件一行中的所有变量。 这是代码:

命名空间WindowsFormsApp2 {     公共课Biblotekarie     {

    public void inläsning()
    {



    }

}

public partial class Form1 : Form
{

    List<string> Spara = new List<string>();



    string[] animals2 = new string[] { "Författare Pippi långstrump, Roman, Årstyp"
        , "Författare Pippi långstrump, Roman, Årstyp"
        , "Tue", "Wed", "Thu", "Fri", "sat" };

    public static Random rng;


    public Form1()
    {

        InitializeComponent();
        StreamReader file = new StreamReader(@"C:\Users\razer\Desktop\test.txt", Encoding.Default, false);
        string item = "";
        while ((item = file.ReadLine()) != null)
        {

            Spara.Add(item);
        }
        //Spara.Add(animals2);
        rng = new Random();

        foreach (string a in Spara)
        {
            string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
        }
    }

    private void Button1_Click(object sender, EventArgs e)
    {

            int index = rng.Next(0,4);

        foreach (string item in Spara)

        {

            textBox1.Text = Convert.ToString(item[index]);

        }



    }

    private void SökRuta(object sender, EventArgs e)
    {

    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要再次使用rng才能在数组中获取随机项

textBox1.Text = Convert.ToString(item[rng.Next(0, 2), rng.Next(0, 2)]);