gpt4 book ai didi

c# 从数组中获取最小值

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:12 25 4
gpt4 key购买 nike

我无法从数组中获取最小值。每次运行它时,最小值仍然为零。我知道索引必须减一,但我不知道应该如何将它应用到代码中。抱歉我的英语不好,我希望你们能帮助我!

public partial class Form1 : Form
{
int[] numbers = new int[99];
int index = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
public int Som()
{
//if the numbers are <99 they will be add to index.
int som = 0;
for (int i = 0; i < 99; i++)
{
som = som + numbers[i];
}
return som;
}
public int Average()
{
//
int answersom = Som();
int g = (answersom / index);
return g;
}
public int Max()
{
int Max = numbers[0];
foreach (int number in numbers)
{
if (number > Max)
{
Max = number;
}
}
return Max;
}
public int Min()
{// array gets de value thats on 0
int Min = numbers[0];
foreach (int number in numbers)
{// if the number is smaller then 0.
if (number < Min)
{// zero is the new number
Min = number;
}

}
return Min;
}
private void button1_Click(object sender, EventArgs e)
{
//if textbox doesnt contain numbers
if (textBox1.Text.All(chr => char.IsLetter(chr)))
{
//labeltext message 'please only fill in numbers'
label1.Text = "Vul alleen Cijfers in";
}
//else
else
{
//is putting the numbers on the right spot of the array
numbers[index] = Convert.ToInt32(textBox1.Text);
//index will add each time.
index++;
label4.Text = Convert.ToString(Som());

}
//
int g = Convert.ToInt32(textBox1.Text);
label5.Text = Convert.ToString(Average());
{

label6.Text = Convert.ToString(Min());

label7.Text = Convert.ToString(Max());

}
}
}

最佳答案

您可以轻松地使用 Linq,

使用系统.Linq;

int min = numbers.Min();

关于c# 从数组中获取最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com