简体   繁体   English

命令行不显示所有输出

[英]Command Line does not show all of the output

I have a 2-Dimensional array and a loop that prints all of the elements in the array using Console.Write and Console.WriteLine. 我有一个二维数组和一个循环,该循环使用Console.Write和Console.WriteLine打印该阵列中的所有元素。 However, the command line does not show all of the elements of the array. 但是,命令行未显示数组的所有元素。 I tried debugging by also printing the output to a text file. 我也尝试通过将输出打印到文本文件来进行调试。 When I look at the text file I see that it contains part of the expected output and the command line contains the other part of the expected output with a few overlaps. 当我查看文本文件时,我看到它包含预期输出的一部分,而命令行包含预期输出的另一部分,但有一些重叠。 Does anyone know a possible solution to this problem? 有谁知道这个问题的可能解决方案?

public void Print()
{
    TextWriter tw = new StreamWriter("output.txt");
    //Prints the accessed coordinates and how many times it was accessed
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 25; j++)
        {
            Console.Write("Coordinate " + grid[i, j].getCoordinates() + ": ");
            Console.WriteLine(grid[i, j].getAccessed()); 
            string text = "Coordinate " + grid[i,j].getCoordinates() + ": " + grid[i,j].getAccessed() + "";
            tw.WriteLine(text);
        }
    }
}

I think you are exceeding how many lines the console can display. 我认为您已经超出了控制台可以显示的行数。

Try this: Console.BufferHeight = 500; 试试这个: Console.BufferHeight = 500;

I don't catch any errors in your code, However you can do two things. 在您的代码中我没有发现任何错误,但是您可以做两件事。

Enable AutoFlush before doing any writes to the stream and check out the file. 在对流进行任何写入并检出文件之前,请启用AutoFlush

1. 1。

TextWriter tw = new StreamWriter("c:\\textwriter.txt");
tw.AutoFlush = true;

or 要么

public void Print()
{
    TextWriter writeFile = new StreamWriter("c:\\textwriter.txt");

    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 25; j++)
        {
            writeFile.WriteLine("Coordinate " + grid[i,j].getCoordinates() + ": " + grid[i,j].getAccessed() + "");
            writeFile.Flush();
        }
    }
    writeFile.Close();
    writeFile = null;

}

2.Implement exception handling. 2.实施异常处理。

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

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