C/C++、Java、Python 等控制台程序在 IDE(如 Visual Studio、Code::Blocks、Eclipse)中直接運行時,進程結束後控制台窗口自動關閉,導致看不到輸出結果。
| 語言/環境 | 方法 |
|---|---|
| C/C++ (Windows) | 在 main 末尾添加 system("pause"); 或 getchar(); |
| C/C++ (跨平台) | printf("Press Enter to exit..."); getchar(); |
| Java | IDE 通常有「保持控制台」選項;或代碼末尾 new Scanner(System.in).nextLine(); |
| Python | input("Press Enter to exit...") 或 IDE 配置「Run in console」 |
| Visual Studio | 項目屬性 → Linker → System → SubSystem: Console (/SUBSYSTEM:CONSOLE) 已默認;調試 → 「開始執行(不調試)」Ctrl+F5 會自動暫停 |
getchar()/input()),避免依賴 system("pause")(僅 Windows 有效且有安全風險)。#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello, World!\n");
#ifdef _WIN32
system("pause"); // 僅 Windows 有效
#else
printf("Press Enter to continue...");
getchar();
#endif
return 0;
}