C#,使用方法查找最小值和最大值

时间:2018-10-14 02:23:29

标签: c# methods

我需要为Button_Click事件编写四种方法

  1. 一个为15个元素的数组中的所有元素随机生成介于1-100之间的值。
  2. 找到最大int并打开一个MessageBox以显示返回值的人
  3. 一个找到最小int并打开MessageBox来显示返回值的对象。
  4. 一个按降序对数组进行排序并打开MessageBox以显示返回值的函数。

我在获取maximumIntButton方法和minimumIntButton_click方法以获取我的randomGenerateButton_Click方法中生成的值时遇到问题。谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Array_Argument
{ 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Click event handler for the goButton control.
        private void goButton_Click(object sender, EventArgs e)
        {
            // Create an int array.
            int[] numbers = { 1, 2, 3 };

            // Display the array in the list box.
            outputListBox.Items.Add("The array's original contents:");
            foreach (int number in numbers)
            {
                outputListBox.Items.Add(number);
            }
            RandomNumberGenerator();
        }

        // Code for random number generation method
        public void RandomNumberGenerator()
        {
            // Create an array to hold the numbers.
            const int SIZE = 15;
            int[] randomNumbers = new int[SIZE];

            // Create a Random object
            Random rand = new Random();   

            // Fill the array with random numbers, in the range
            // of 0 through 99.
            for (int index = 0; index < randomNumbers.Length; index++)
            {
                // Assign a random number between 0-99 to randomNumbers array
                randomNumbers[index] = rand.Next(100);
            }
            // Repeats 15 times
            // Display the current array value in the box
            outputListBox.Items.Add("15 random numbers have been assigned");
        }

        // Click event handler for the exitButton control.
        private void exitButton_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }

        private void findMaxButton_Click(object sender, EventArgs e)
        {               
            int largest = randomNumbers[14];
            for (int i = 0; i < 15; i++)
            {   
                if (randomNumbers[i] > largest)
                {
                    largest = randomNumbers[i];   
                }
            }
            outputListBox.Items.Add("The largest number is: " + largest);    
        }

        private void findMinButton_Click(object sender, EventArgs e)
        {             
            int smallest = randomNumbers[14];
            for (int i = 0; i < 15; i++)
            {   
                if (randomNumbers[i] > smallest)
                {
                    smallest = randomNumbers[i];   
                }
            }
            outputListBox.Items.Add("The smallest number is: " + smallest);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应将“ SIZE”和“ randomNumbers”的声明移至“ RandomNumberGenerator”方法之外。这样,任何函数/事件处理程序都可以访问它们。

您的代码应以以下内容开头:

public Form1()
{
    InitializeComponent();
}

const int SIZE = 15;
int[] randomNumbers = new int[SIZE];

// Click event handler for the goButton control.

现在从“ RandomNumberGenerator”内部删除它们。然后,您可以从事件处理程序中访问它们。