随机统计应用程序?(控制台C#)

本文关键字:控制台 统计 应用程序 随机 | 更新日期: 2023-09-27 18:23:53

使用C#控制台创建一个随机统计应用程序,要求用户输入数字。然后,它应该生成0到9之间的许多随机数,然后显示每个数的出现次数。应用程序输出应输出:

Enter a number: 500
Number  Occurences
0       58
1       56
2       47
3       49
4       49
5       55
6       49
7       59 
8       36 
9       42
1. Assign New Set of Integers  
2. Exit

到目前为止,我得到的是:

string input;
int userNumber;
Console.Write("Enter a number: ");
input = Console.ReadLine();
userNumber = int.Parse(input);
Console.WriteLine("'n" + "Number Occurrences");
int[] number = new int[10]; 
int[] numberTwo = new int[userNumber]; 
Random randNum = new Random();
for (int counter = 0; counter < userNumber; counter++)
 {
numberTwo[counter] = randNum.Next(10);
if (numberTwo[counter] == 0)
{
number[0]++;
}
if (numberTwo[counter] == 1) 
{
number[1]++;
}
if (numberTwo[counter] == 2)
{
number[2]++;
}
if (numberTwo[counter] == 3) 
{
number[3]++;
}
if (numberTwo[counter] == 4)
{
number[4]++;
}
if (numberTwo[counter] == 5)
{
number[5]++;
}
if (numberTwo[counter] == 6)
{
number[6]++;
}
if (numberTwo[counter] == 7)
{
number[7]++;
}
if (numberTwo[counter] == 8)  
{
number[8]++;
}
if (numberTwo[counter] == 9)
{
number[9]++;
}
} 
for (int numberThree = 0; numberThree <= 9; numberThree++)
{
Console.WriteLine(numberThree + "'t" + number[numberThree]);
}
Console.ReadKey();

正如你所看到的,我的老师问题的第一部分已经开始了,但当谈到下一部分时,我似乎被难住了,在下一部分中,我必须添加一个功能,用户可以键入1来输入一个新数字,或者按2退出程序。(程序在控制台btw)

随机统计应用程序?(控制台C#)

我会将它包装在while循环中,并将您的逻辑移动到一个单独的方法中。

class Program
    {
        static void Main(string[] args)
        {
            EnterNumber();
            Console.ReadKey();
            bool doEnd = false;
            while (!doEnd)
            {
                Console.WriteLine("");
                Console.WriteLine("1.Assign New Set of Integers");
                Console.WriteLine("2.Exit");
                ConsoleKeyInfo info = Console.ReadKey();
                int val;
                if (!int.TryParse(info.KeyChar.ToString(), out val))
                {
                    // Error handling...
                }
                switch (val)
                {
                    case 1: EnterNumber();
                        break;
                    case 2:
                        doEnd = true;
                        break;
                }
            }
        }
        private static void EnterNumber()
        {
            string input;
            int userNumber;
            Console.Write("Enter a number: ");
            input = Console.ReadLine();
            userNumber = int.Parse(input);
            Console.WriteLine("'n" + "Number Occurrences");
            int[] number = new int[10];
            int[] numberTwo = new int[userNumber];
            Random randNum = new Random();
            for (int counter = 0; counter < userNumber; counter++)
            {
                numberTwo[counter] = randNum.Next(10);
                if (numberTwo[counter] == 0)
                {
                    number[0]++;
                }
                if (numberTwo[counter] == 1)
                {
                    number[1]++;
                }
                if (numberTwo[counter] == 2)
                {
                    number[2]++;
                }
                if (numberTwo[counter] == 3)
                {
                    number[3]++;
                }
                if (numberTwo[counter] == 4)
                {
                    number[4]++;
                }
                if (numberTwo[counter] == 5)
                {
                    number[5]++;
                }
                if (numberTwo[counter] == 6)
                {
                    number[6]++;
                }
                if (numberTwo[counter] == 7)
                {
                    number[7]++;
                }
                if (numberTwo[counter] == 8)
                {
                    number[8]++;
                }
                if (numberTwo[counter] == 9)
                {
                    number[9]++;
                }
            }
            for (int numberThree = 0; numberThree <= 9; numberThree++)
            {
                Console.WriteLine(numberThree + "'t" + number[numberThree]);
            }
        }
    }

请注意,如果用户从不按2,这将是一个无限循环。此外,请更改int.Parse,因为它可能会使您的应用程序崩溃。请尝试使用int.TryParse

https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

正如评论中提到的,您可以通过使用字典来优化代码。

private static void UseDict()
{
    Console.Write("Enter a number: ");
    string input = Console.ReadLine();
    int number;
    if (!int.TryParse(input, out number))
    {
        // Error handling here.
    }
    var userNumber = int.Parse(input);
    Console.WriteLine("'n" + "Number Occurrences");
    var dict = new Dictionary<int, int>();
    Random randNum = new Random((int)DateTime.Now.Ticks);
    for (int counter = 0; counter < userNumber; counter++)
    {
        var generatedNumber = randNum.Next(0, 10);
        if (!dict.ContainsKey(generatedNumber))
        {
            // Create key.
            dict.Add(generatedNumber, 0);
        }
        // Increase count.
        dict[generatedNumber]++;
    }
    for (int i = 1; i < 10; i++)
    {
        var count = dict.ContainsKey(i) ? dict[i] : 0;
        Console.WriteLine("{0} - {1}", i, count);
    }
    Console.ReadKey();
}