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

通过控制台应用程序将C#输出随机化,c#,C#,我对C#编码一无所知,有人要求我创建一个简单的应用程序,方法是获取一个对象并随机输出。可以将其视为控制台应用程序中的2D映射 到目前为止,我得到的是: static void Main(string[] args) { List<Position> positions = new List<Position>(); for (int i = 0; i < 100; i++) { Position tempPo

我对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);
    }
}
static void Main(字符串[]args)
{        
列表位置=新列表();
对于(int i=0;i<100;i++)
{
位置tempPosition=新位置();
tempPosition.x=i;
tempPosition.y=i;
位置。添加(临时位置);
}
}

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

如注释所示,您应该使用
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();
    }
static void Main(字符串[]args)
{
//创建Random类的实例。我们将使用
//生成随机数。
随机rnd=新随机();
//我们的随机位置列表。
列表位置=新列表();
//使用“Console.WindowWidth”和创建100个随机位置
//`Console.WindowHeight`在控制台屏幕上选择一个随机位置。
对于(int i=0;i<100;i++)
{
位置tempPosition=新位置();
tempPosition.X=rnd.Next(控制台窗口宽度);
tempPosition.Y=rnd.Next(控制台窗口高度);
位置。添加(临时位置);
}
//对于我们随机生成的每个位置
foreach(位置中的位置)
{
//将光标移动到屏幕上的该位置
控制台。设置光标位置(位置X、位置Y);
//再次使用“Random”类随机选择哪个字符
//在这种情况下,每个字符大约有一个
//有50%的机会被选中。
如果(rnd.Next(100)>=50)
{
控制台。写($);
}
其他的
{
控制台。写(“@”);
}
}
//这将阻止程序退出,直到我们按enter键。
Console.ReadLine();
}
控制台中任意位置的随机碎片

举个简单的例子:

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("@");
    }
}
类程序
{
静态随机R=新随机();
静态void Main(字符串[]参数)
{
列表位置=新列表();
对于(int i=0;i<100;i++)
{
位置tempPosition=新位置();
tempPosition.x=R.Next(控制台窗口宽度);
tempPosition.y=R.Next(控制台窗口高度-1);
//…设置tempPosition的其他属性。。。
位置。添加(临时位置);
}
绘图(位置);
Console.SetCursorPosition(0,Console.WindowHeight-1);
控制台。写入(“按Enter键退出”);
Console.ReadLine();
}
静态void DrawMap(列表mapData)
{
Console.Clear();
foreach(地图数据中的位置p)
{
p、 Draw();
}
}
}
阶级地位
{
公共int x;
公共智力;
公众抽签()
{
控制台。设置光标位置(x,y);
控制台。写(“@”);
}
}
输出:

这里什么是随机类?利用类?…正如@Idle\u Mind上面所说的利用随机类-但是要知道它不是真正的随机类。请看这篇博文了解更多信息:您在发布的代码中使用了所有花括号有什么原因吗?@Hapex这是新的!谢谢你,这对我帮助很大,因为我很难用这个:static Random R=new Random();tempPosition.x=R.Next(控制台窗口宽度);tempPosition.y=R.Next(Console.WindowHeight-1)