简体   繁体   English

(C#)如何从随机数生成器中获取数字并在消息框中输出最高和最低数字?

[英](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. 我想我需要以某种方式使用if / else但是有点困惑。 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 如果将所有数字放在集合中,则可以使用LINQ to Objects扩展方法MinMax

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. 因为在生成完整列表之前无法获得最小值或最大值,所以该代码需要在for循环之外,并且您需要将所有随机值放在集合中以便它们保持不变。 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. 此外,如果要弹出消息框,则在Visual Studio中创建项目时,应该创建Windows窗体应用程序而不是控制台应用程序。 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. 如果您正在使用winforms,您可以只使用MessageBox.Show("My message here")但如果您已经构建了一个控制台应用程序,则必须包含一堆程序集才能使其工作。

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 : 要在控制台应用程序中显示消息框,您需要设置对System.Windows.Forms的引用,并在正确的using语句之后:

            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; 您也在使用控制台应用程序和可能在Windows窗体中使用的MessageBox ,但如果要在控制台应用程序中使用它,则需要导入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: 然后你在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: 另一种方法是使用Linq的Aggregate方法:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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