mercredi 22 novembre 2017

Homework help: Total sum of random array values in C#?

So we created an application in our last chapter that generated and displayed a set of random numbers ranging from 0-99. For our "new" assignment, we are being asked to add a label and textbox to present the total of the random values that are displayed each time we generate a new set. I've found the section in my textbook that describes how the code is written to total the values, but not when they are randomized. I've been racking my brain trying to search for a solution online but either the programming language is different or the values being totaled aren't randomized. Can someone walk me through how to go about this?

    private void generateButton_Click(object sender, EventArgs e)
    {
        // Create an array to hold the numbers.
        const int SIZE = 5;
        int[] lotteryNumbers = 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 < lotteryNumbers.Length; index++)
        {
            lotteryNumbers[index] = rand.Next(100);
        }

        // Display the array elements in the Label controls.
        firstLabel.Text = lotteryNumbers[0].ToString();
        secondLabel.Text = lotteryNumbers[1].ToString();
        thirdLabel.Text = lotteryNumbers[2].ToString();
        fourthLabel.Text = lotteryNumbers[3].ToString();
        fifthLabel.Text = lotteryNumbers[4].ToString();
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        // Close the form.
        this.Close();
    }




Aucun commentaire:

Enregistrer un commentaire