简体   繁体   中英

(C#) How can I take numbers from my random number generator and output the highest and lowest number in a message box?

I have a random number generator and for an assignment we have to take the random numbers and make outputs in a messagebox of the highest and lowest number. I think I need to use if/else somehow but am kind of confused. My code as of now looks like:

class Program
{
    static void Main(string[] args)
    {
        Random random = new Random();
        int randomNumber;
        for (int i = 0; i < 11; i++)
        {
            randomNumber = random.Next(1000);
            Console.WriteLine(randomNumber);
        }         
    }
}

If you put all the numbers in a collection you can use the LINQ to Objects extension methods Min and Max

Random random = new Random();
List<int> randos = new List<int>();
for (int i = 0; i < 11; i++)
{
    randos.Add(random.Next(1000));
}

int min = randos.Min();
int max = randos.Max();

Console.WriteLine("The minimum value is " + min);
Console.WriteLine("The maximum value is " + max);

Because you cannot get the min or max until you've generated the full list, that code needs to be outside of the for loop and you need to put all the random values in a collection so that they persist. I think your problem lies in an attempt to do it all a streaming manner when you must first have a fully formed collection.

Also, if you want to pop up a message box then you should probably create a Windows Forms App rather than a Console Application when creating your project in Visual Studio. If you're working with winforms you can just do MessageBox.Show("My message here") but if you've built a console application you'll have to include a bunch of assemblies to make that work.

If all you care about is just both minimum and maximum of a series of numbers, without storing each one of them, you can just hold the current maximum and minimum on two variables, and update them as the loop progresses. After the final iteration you'll get the max and min of the whole lot:

static void Main(string[] args)
{
    Random random = new Random();
    int maxNumber;
    int minNumber;
    maxNumber = minNumber = random.Next(1000);   // Assign both variables at once
    for (int i = 0; i < 11; i++)
    {
        int randomNumber = random.Next(1000);
        Console.WriteLine(randomNumber);
        if (randomNumber > maxNumber) maxNumber = randomNumber;
        if (randomNumber < minNumber) minNumber = randomNumber;
    }
    Console.WriteLine("Maximum: {0}", maxNumber);
    Console.WriteLine("Minimum: {0}", minNumber);
    Console.ReadKey(true);
}

To show the messagebox in console application you need to set reference to System.Windows.Forms and after the proper using statement then :

            Random random = new Random();
            List<int> randomNumbers = new List<int>();
            for (int i = 0; i < 11; i++)
            {
                randomNumbers.Add(random.Next(100000));//or set to your desired value
            }

            //Console.WriteLine("Biggest number is {0} -smallest is {1}", randomNumbers.Max(), randomNumbers.Min());
            MessageBox.Show("Biggest number is " + randomNumbers.Max().ToString() + "- smallest is " + randomNumbers.Min().ToString() );

You just need to gather all random numbers to select the minimum and maximum from them. also you are using Console Application and the MessageBox probably used in Windows Forms but if you want to use it in Console Application you need to import the using System.Windows.Forms; library to use it just by Select:

Project->Add Reference  

From the left side Select

FrameWork

and then choose

System.Windows.Forms

and then at the beginning of your code write:

using System.Windows.Forms;

Finally click

OK

and then your code in the Main:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            List<int> randomNumbers= new List<int>();
            for (int i = 0; i < 11; i++)
            {
                randomNumbers.Add(random.Next(1000));
            }

            MessageBox.Show(string.Format("The minimum is: {0}\nThe maximum is: {1}", randomNumbers.Min(), randomNumbers.Max()), "Result");

        }
    }
}

Another method would be to use Linq's Aggregate method:

var random = new Random();
var limits = 
    Enumerable.Range(0, 11)
              .Select(x => random.Next(1000))
              .Aggregate(new { min = int.MaxValue, max = int.MinValue }, 
              (a, x) => new 
              { 
                  min = Math.Min(a.min, x), 
                  max = Math.Max(a.max, x) 
              });
MessageBox.Show(string.Format("Min: {0}, Max: {1}", limits.min, limits.max));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM