c# - 使用 while 或 for? 查找 array 中的最大数字?

标签 c# arrays

请给我一个问题,我需要找到数组中的最高值。人们将向数组写入名称 (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/

相关文章:

c# - NHibernate 无法匹配 DateTime 对象

javascript - 使用 JavaScript 将 4 位字节数组转换为 int

c++ - 无法填充动态类模板数组

java - 不理解二维数组的每个循环

javascript - array.push() 方法抛出 TypeError

c# - 将 XAML 转换为 C#

c# - ViewComponents 不是异步的

c# - 类继承命名

javascript - 如何使用变量更改 jquery 数据表语言

c - 为什么我的 char 数组有一半是空白?