【发布时间】:2017-09-14 14:05:58
【问题描述】:
我正在做一个包含彩票号码生成器的项目,然后将其保存到txt 文件中,但我有一个问题:当每个号码在不同的标签中时,如何按升序对号码进行排序?
代码
static Random Gerar = new Random();
public static readonly string FilePath = "ChavesEuromilhões.txt";
public GerarChave()
{
InitializeComponent();
}
private void gerarNumeros() // method to generate numbers
{
int a = Gerar.Next(1, 50);
int b = Gerar.Next(1, 50);
int c = Gerar.Next(1, 50);
int d = Gerar.Next(1, 50);
int e = Gerar.Next(1, 50);
int f = Gerar.Next(1, 12);
int g = Gerar.Next(1, 12);
num1.Content = a.ToString();
num2.Content = b.ToString();
num3.Content = c.ToString();
num4.Content = d.ToString();
num5.Content = e.ToString();
num6.Content = f.ToString();
num7.Content = g.ToString();
}
private void button_voltar_Click(object sender, RoutedEventArgs e)
{
MainWindow sw = new MainWindow();
sw.Show();
this.Close();
}
private void button_Guardar_Click(object sender, RoutedEventArgs e) // save the number
{
using (StreamWriter writer = new StreamWriter(GerarChave.FilePath, true))
{
if (num1.Content == null || num2.Content == null || num3.Content == null || num4.Content == null || num5.Content == null || num6.Content == null || num7.Content == null || comboBox.SelectedValue == null || Date_Picker.SelectedDate == null)
{
System.Windows.MessageBox.Show("Todos Os campos São de Prenchimento Obrigatorio!");
}
else
{
writer.WriteLine(num1.Content + "," + num2.Content + "," + num3.Content + "," + num4.Content + "," + num5.Content + "," + num6.Content + "," + num7.Content + "," + comboBox.SelectedValue + "," + Date_Picker.SelectedDate.Value.Date.ToShortDateString());
writer.Close();
System.Windows.MessageBox.Show("Chave Guardada!");
}
}
}
private void button_ordenar_Click(object sender, RoutedEventArgs e)
{
// button to order the numbers
}
}
}
}
【问题讨论】:
-
将 num1.Conetent 和 num7.Content 放入列表并使用 linq
-
将号码添加到列表中,然后调用
Sort()