C# - 偵錯主控台無法將"Console.ReadLine();"儲存的字串(中文)輸出成中文字串

柏勳 陳 20 Reputation points
2023-07-14T07:22:32.4766667+00:00

程式碼如下:

string name_1 = "派大星";

Console.Write("請輸入您的姓名 : ");

string name_2 = Console.ReadLine(); // Input Traditional Chinese Characters

Console.WriteLine("name_1 is : " + name_1);

Console.WriteLine("name_2 is : " + name_2);

我在第三行程式碼輸入了"流水",但是卻在下圖中輸出了 "<?><?>"

" 請問我檢查過我的主控台及電腦的輸入及輸出都是 UTF-8(65001),那為何會輸出亂碼呢?? "

謝謝.

亂碼

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2023-07-14T08:19:11.07+00:00

    Hi,@柏勳 陳.

    Confirm that the console font supports Chinese characters:

    Right-click on the console window title bar.

    Select "Properties."

    Go to the "Font" tab.

    Choose a font that supports Chinese characters, such as "Consolas" or "SimSun."

    If changing the font doesn't resolve the issue, you can try a different approach to read and display Chinese characters using the console:

    
                Console.InputEncoding = Encoding.Unicode;
                Console.OutputEncoding = Encoding.Unicode;
                string name_1 = "派大星";
    
                Console.Write("請輸入您的姓名 : ");
    
                string name_2 = Console.ReadLine();
    
                Console.WriteLine("name_1 is : " + name_1);
                Console.WriteLine("name_2 is : " + name_2);
                Console.ReadKey();
    
    
    

    The result:

    User's image

    By the way, this is an English forum, please ask questions in English.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.