Console.Clear()不会清除整个控制台窗口

时间:2013-12-01 12:52:26

标签: c# console console-application clear

我正在编写一个简单的程序,我想用Console.Clear()清除整个控制台窗口,但它只删除我在同一方法中编写的行。它不会删除前面的行。

有什么想法吗?

代码(第123行)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Szigeti_Marton_FW0S9E
{
    //Reversi (Othello) - Szigeti Márton, FW0S9E

    class Game
    {
        private int[,] Table { get; set; }

        private int gameCounter { get; set; }

        private const int dimX = 8;
        private const int dimY = 8;

        private const char playerSign = 'X';
        private const char computerSign = 'O';

        private void Map()
        {
            Console.Write("  ");

            for(int k = 0; k < 8; k++)
                Console.Write("{0} ", (char)(65 + k));

            Console.WriteLine();

            for (int i = 0; i < dimX; i++)
            {
                Console.Write(i+1);

                for (int j = 0; j <= dimY; j++)
                {
                    Console.Write("| ");
                }

                Console.WriteLine();
            }
        }

        private void Map(int x, int y, char sign)
        {
            //Overloaded

            Console.Write("  ");

            for (int k = 0; k < 8; k++)
                Console.Write("{0} ", (char)(65 + k));

            Console.WriteLine();

            for (int i = 0; i < dimX; i++)
            {
                Console.Write(i + 1);

                for (int j = 0; j <= dimY; j++)
                {
                    Console.Write("| ");
                }

                Console.WriteLine();
            }
        }

        private bool isFieldEmpty(int x, int y)
        {
            if (Table[x, y] == -1)
                return true;
            else
                return false;
        }

        private bool isInputValid(string input)
        {
            bool temp = true;

            bool tempLetter = false, tempNumber = false;

            if ((input.Length == 2) && (char.IsLetter(input[0])) && (char.IsNumber(input[1])))
            {

                for (int i = 0; i < dimX; i++)
                {
                    if (input[0] == (char)65 + i)
                        tempLetter = true;
                }

                if (!tempLetter)
                    temp = false;

                for (int j = 0; j < dimY; j++)
                {
                    if (int.Parse(input[1].ToString()) == j)
                        tempNumber = true;
                }

                if (!tempNumber)
                    temp = false;
            }
            else if (input.Length != 2)
                temp = false;

            return temp;
        }

        private void makeAMove()
        {
            int tempLetter = 0;

            gameCounter++;
            string Field;
            Random randomMove = new Random();

            Console.WriteLine("Adjon meg egy mezőt, pl. \"D3\". Feladáshoz: \"feladom\"");

            do { Field = Console.ReadLine(); }
            while (!isInputValid(Field));

            Console.Clear(); // <-- Line 123

            if (gameCounter % 2 == 1)
            {
                Field[0].ToString().ToUpper();

                for(int i = 0; i < 8; i++)
                    if(Field[0] == (char)(65+i))
                        tempLetter = i;

                Map(Field[1], tempLetter, playerSign);
            }
            else if (gameCounter % 2 == 0)
            {
                //Gép random lépés
            }
        }

        static void Main(string[] args)
        {
            Game Player1 = new Game();
            Player1.Map();
            Player1.makeAMove();

            Console.ReadLine();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

Console.Clear()真正起作用的原因是因为方法中的do-while语句。 (第120行和第121行)。

Console.Clear();移到do-while语句上方(可能是第120行),它会起作用。

do-while阻塞Console.clear()的原因是在do语句中,它期望输入,而在while语句中,它期望输入满足条件

do { Field = Console.ReadLine(); } 
while (!isInputValid(Field));) 

没有那个输入,直到满足条件,它就不会进入下一个语句。

答案 1 :(得分:0)

    static void Main()
    {
        Console.WriteLine("0");
        Test1();
        Test2();
    }

    public static void Test1()
    {
        Console.WriteLine("1");
    }

    public static void Test2()
    {
        Console.WriteLine("2");
        Console.Clear();
        Console.WriteLine("3");
    }

输出是:

3

问题不在Console.Clear,而在于你的逻辑

答案 2 :(得分:-1)

希望这个帮助:

 using System;
using System.Runtime.InteropServices;

namespace nsClearConsole
{
   /// <summary>
  /// Summary description for ClearConsole.
  /// </summary><BR/>
   public class ClearConsole
  {     
     private const int STD_OUTPUT_HANDLE  = -11;
     private const byte EMPTY = 32;

     [StructLayout(LayoutKind.Sequential)]
     struct COORD
     {
        public short x;
        public short y;
     }

     [StructLayout(LayoutKind.Sequential)]
     struct SMALL_RECT
     {
        public short Left;
        public short Top;
        public short Right;
        public short Bottom;
     }

     [StructLayout(LayoutKind.Sequential)]
     struct CONSOLE_SCREEN_BUFFER_INFO
     {
        public COORD dwSize;
        public COORD dwCursorPosition;
        public int wAttributes;
        public SMALL_RECT srWindow;
        public COORD dwMaximumWindowSize;
     }

     [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
     private static extern int GetStdHandle(int nStdHandle);

     [DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
     private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);

     [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
     private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

     [DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
     private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);

     private int hConsoleHandle;

     public ClearConsole()
     {
        // 
        // TODO: Add constructor logic here.
        // 
        hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
     }

     public void Clear()
     {
        int hWrittenChars = 0;
        CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();           
        COORD Home;     
        Home.x = Home.y = 0;
        GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
        FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);
        SetConsoleCursorPosition(hConsoleHandle, Home);
     }
  }
}

有关详细信息,请参阅此内容:      http://support.microsoft.com/kb/319257