简体   繁体   English

异常Console.Write行为

[英]Unusual Console.Write behaviour

Console.Write isn't behaving as it should; Console.Write的行为不正常。 I'm trying to create a class that handles drawing to specific regions of the console, and some really odd stuff's been happening. 我正在尝试创建一个类来处理到控制台特定区域的绘图,并且发生了一些非常奇怪的事情。 I've got no clue how to debug it. 我不知道如何调试它。

When I throw this code in: 当我将此代码放入:

string output;

for (int i = 0; i < Console.WindowHeight; ++i) {
    Console.SetCursorPosition(0, i);
    output = "";
    for (int j = 0; j < Console.WindowWidth; ++j) output += (i % 10).ToString();
    Console.Write(output);
}

I get the following: http://snag.gy/LrFcU.jpg (I'm not allowed to post images because of reputation limits -.-) 我得到以下信息: http : //snag.gy/LrFcU.jpg (由于声誉限制-.-而被禁止发布图像)

Yet when I modify it like so: 但是当我这样修改它时:

string output;

for (int i = 0; i < Console.WindowHeight; ++i) {
    Console.SetCursorPosition(0, i);
    output = "";

    // take one character away from the string
    for (int j = 0; j < Console.WindowWidth - 1; ++j) output += (i % 10).ToString();

    Console.Write(output);
}

I get this! 我明白了! http://snag.gy/jxLH8.jpg http://snag.gy/jxLH8.jpg

WindowWidth is 80, WindowHeight is 25, and both buffer width and height are equal to window width and window height. WindowWidth是80,WindowHeight是25,并且缓冲区的宽度和高度都等于窗口的宽度和窗口的高度。

I'm resetting the cursor position every interation of the outer for loop... Why would it be omitting the first line? 我在外部for循环的每个交互位置都重置了光标位置...为什么会省略第一行?

The first line isn't omitted, it's just that it scrolled out of sight. 第一行并没有被忽略,只是它滚动到了视线之外。

You're adding one more character in the first version, thus reaching the end of the line and putting the caret on the next line. 您将在第一个版本中再添加一个字符,从而到达该行的末尾,并将插入号置于下一行。

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

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