请给我一个问题,我需要找到数组中的最高值。人们将向数组写入名称 (textbox1) 和金钱 (texbox2)。我有 2 个按钮,第一个按钮保存到数组中,第二个按钮用最大的钱写名字。
保存代码:
string[] name = new string[50];
int items = 0;
int[] money = new int[50];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Convert.ToInt32(textBox2.Text);
}
catch
{
name[items] = textBox1.Text;
money[items] = Int32.Parse(textBox2.Text);
items++;
}
}
而对于button2需要查找最大值并写入名称!请帮助我
最佳答案
private void button2_Click(object sender, EventArgs e)
{
int maxIndex = 0;
for(int i = 0; i < 50; i++)
{
if (money[i] > money[maxIndex])
maxIndex = i;
}
MessageBox.Show(name[maxIndex] + " has biggest value " + money[maxIndex]);
}
关于c# - 使用 while 或 for? 查找 array 中的最大数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593006/