簡體   English   中英

按Enter鍵時,Visual Studio執行關閉

[英]Visual Studio execution closes when pressing enter

你好我試着一步一步地學習C#。 我安裝Visual Studio進行練習,但是在執行時我無法測試我的基本代碼20分鍾:

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("My name is " + name);
        }
    }
}

這是基本的,但當我執行並輸入名稱並按Enter鍵時,cmd就會關閉。 任何幫助將不勝感激,因為我熱衷於開始使用C#

CMD

Main方法返回

Console.WriteLine("My name is " + name);

這有效地終止了應用程序。 你應該放一個

Console.Read();

等到下一次擊鍵。

添加另一個輸入以關閉應用程序,因此cmd將不會關閉,直到您再次按Enter鍵。

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("My name is " + name);
            Console.ReadLine();
        }
    }
}

嘗試Ctrl + F5

如果您不需要調試,則Ctrl-F5是最佳選擇

沒有任何Console.Readline()或ReadKey()自動工作

Visual Studio將使控制台窗口保持打開狀態,直到您按下某個鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM