通过控制台应用程序随机化C#输出

时间:2019-05-23 16:21:50

标签: c#

我对C#编码是完全陌生的,已经被要求通过获取一个对象并随机化输出来创建一个简单的应用程序。在控制台应用程序中将其视为2D地图。

到目前为止,我有:

static void Main(string[] args)
{        
    List<Position> positions = new List<Position>();

    for (int i = 0; i < 100; i++)
    {
        Position tempPosition = new Position();
        tempPosition.x = i;
        tempPosition.y = i;
        positions.Add(tempPosition);
    }
}

只是不确定如何构造它,例如说我想在随机数个空格后写入一个对象,然后在下一行再次执行它,然后执行console.writeline。

2 个答案:

答案 0 :(得分:1)

正如评论所建议的那样,您应该使用Random类来随机选择位置,以及是否绘制@或$符号。

    static void Main(string[] args)
    {
        //Create an instance of the Random class.  We'll use this
        //to generate random numbers.
        Random rnd = new Random();

        //Our list of random positions.
        List<Position> positions = new List<Position>();

        //Create 100 random positions using `Console.WindowWidth` and 
        // `Console.WindowHeight` to pick a random location on the console screen.
        for (int i = 0; i < 100; i++)
        {
            Position tempPosition = new Position();
            tempPosition.X = rnd.Next(Console.WindowWidth);
            tempPosition.Y = rnd.Next(Console.WindowHeight);
            positions.Add(tempPosition);
        }

        //For each of our randomly generated positions
        foreach (Position pos in positions)
        {
            //Move the cursor to that position on the screen
            Console.SetCursorPosition(pos.X, pos.Y);

            //Use the `Random` class again to randomly pick which character
            //to write to the screen.  In this case, each character has about a 
            //50% chance of getting chosen.
            if (rnd.Next(100) >= 50)
            {
                Console.Write("$");
            }
            else
            {
                Console.Write("@");
            }

        }

        //This keeps the program from exiting until we press enter.
        Console.ReadLine();
    }

答案 1 :(得分:1)

  

基本上在控制台内的任意位置

一个简单的示例,然后:

class Program
{

    static Random R = new Random();

    static void Main(string[] args)
    {
        List<Position> positions = new List<Position>();

        for (int i = 0; i < 100; i++)
        {
            Position tempPosition = new Position();
            tempPosition.x = R.Next(Console.WindowWidth);
            tempPosition.y = R.Next(Console.WindowHeight - 1);
            // ... set other properties of tempPosition ...
            positions.Add(tempPosition);
        }


        DrawMap(positions);

        Console.SetCursorPosition(0, Console.WindowHeight - 1);
        Console.Write("Press Enter to Quit");
        Console.ReadLine();
    }

    static void DrawMap(List<Position> mapData)
    {
        Console.Clear();
        foreach (Position p in mapData)
        {
            p.Draw();
        }
    }
}

class Position
{
    public int x;
    public int y;

    public void Draw()
    {
        Console.SetCursorPosition(x, y);
        Console.Write("@");
    }
}

输出: enter image description here