教學課程:在 Visual Studio 中建立簡單的 C# 主控台應用程式 (第 1/2 部分)

在本教學課程中,您會使用 Visual Studio 建立並執行 C# 主控台應用程式,以及探索 Visual Studio 整合式開發環境 (IDE) 的一些功能。 本教學課程是兩部分教學課程系列的第 1 部分。

在本教學課程中,您會完成下列工作:

  • 建立 Visual Studio 專案。
  • 建立 C# 主控台應用程式。
  • 偵錯您的應用程式。
  • 關閉您的應用程式。
  • 檢查您的完整程式碼。

在第 2 部分中,您可以擴充此應用程式來新增更多專案、了解偵錯技巧,以及參考協力廠商套件。

必要條件

您必須已安裝 Visual Studio。

如果您尚未安裝 Visual Studio,請前往 Visual Studio 下載頁面免費進行安裝。

建立專案

首先,建立 C# 應用程式專案。 專案類型隨附您需要的所有範本檔案。

  1. 開啟 Visual Studio,然後選取 [開始] 視窗中的 [建立新專案 ]。

    Screenshot that shows the Create a new project window.

  2. 在 [建立新專案] 視窗中, 從語言清單中選擇 [C#]。 接下來,從平台清單中選擇 [Windows],然後從專案類型清單中選擇 [主控台]

    在套用語言、平台和專案類型篩選條件之後,選擇 [主控台應用程式] 範本,然後選取 [下一步]

    注意

    如果您沒有看到 [主控台應用程式] 範本,請選取 [安裝更多工具與功能]

    Screenshot that shows the Install more tools and features link.

    在 Visual Studio 安裝程式 中,選取 .NET Core 跨平台開發工作負載。

    Screenshot showing the .NET Core cross-platform development workload in the Visual Studio Installer.

    在 Visual Studio 安裝程式中選取 [修改]。 系統可能會提示您儲存工作。 選取 [ 繼續] 以安裝工作負載。

    返回此「建立專案」程式中的步驟 2。

  3. 在 [設定您的新專案] 視窗的 [專案名稱] 方塊中鍵入或輸入 Calculator。 然後選取下一步

    Screenshot that shows how to name your project 'Calculator' in the 'Configure your new project' window in Visual Studio.

  4. 在 [其他資訊] 視窗中,確認 .NET Core 3.1 出現在 [目標 Framework] 欄位中。 然後,選取 [建立]

    Screenshot of the Additional information window in Visual Studio showing .NET Core 3.1 as the target framework for the new project.

Visual Studio 會隨即開啟您的新專案,其中包含預設 "Hello World" 程式碼。 若要在編輯器中進行檢視,請在 [方案總管] 視窗中選取程式碼檔案 Program.cs,此視窗通常是在 Visual Studio 右側。

預設「Hello World」程式碼會呼叫 WriteLine 方法,在主控台視窗中顯示常值字串「Hello World!」。 若按 F5,您可以在偵錯模式中執行預設程式。 在應用程式於偵錯工具中執行之後,主控台視窗會保持開啟狀態。 按任意鍵以關閉主控台視窗。

  1. 開啟 Visual Studio,然後選取 [開始] 視窗中的 [建立新專案 ]。

    Screenshot that shows the Create a new project window.

  2. 在 [建立新專案] 視窗中,選取 [所有語言],然後從下拉式清單中選擇 [C#]。 從 [所有平台] 清單中選擇 [Windows],然後從 [所有專案類型] 清單中選擇 [主控台]

    在套用語言、平台和專案類型篩選條件之後,選擇 [主控台應用程式] 範本,然後選取 [下一步]

    注意

    如果您沒有看到 [主控台應用程式] 範本,請選取 [安裝更多工具與功能]

    Screenshot that shows the Install more tools and features link.

    在 Visual Studio 安裝程式 中,選取 .NET 桌面開發工作負載。

    Screenshot showing the .NET desktop development workload in the Visual Studio Installer.

    在 Visual Studio 安裝程式中選取 [修改]。 系統可能會提示您儲存工作。 選取 [ 繼續] 以安裝工作負載。

    返回此「建立專案」程式中的步驟 2。

  3. 在 [設定您的新專案] 視窗中,於 [專案名稱] 方塊中輸入 Calculator,然後選取 [下一步]

    Screenshot that shows how to name your project 'Calculator' in the 'Configure your new project' window in Visual Studio.

  4. 在 [其他資訊] 視窗中,選取 [目標 Framework] 字段的 .NET 8.0。 然後,選取 [建立]

    Screenshot of the Additional information window in Visual Studio showing .NET 8.0 selected as the target framework for the new project.

Visual Studio 會隨即開啟您的新專案,其中包含預設 "Hello World" 程式碼。 若要在編輯器中進行檢視,請在 [方案總管] 視窗中選取程式碼檔案 Program.cs,此視窗通常是在 Visual Studio 右側。

單一程式碼陳述式會呼叫 WriteLine 方法,在主控台視窗中顯示常值字串「Hello World!」。 若按 F5,您可以在偵錯模式中執行預設程式。 在應用程式於偵錯工具中執行之後,主控台視窗會保持開啟狀態。 按任意鍵以關閉主控台視窗。

注意

從 .NET 6 開始,使用主控台範本的新專案會產生與舊版不同的程式碼。 若要深入了解,請參閱新的 C# 範本會產生最上層陳述式頁面。

建立應用程式

在本節中,您會完成下列工作:

  • 探索一些以 C# 撰寫的基本整數運算。
  • 新增程式碼來建立基本計算機應用程式。
  • 偵錯應用程式以找出錯誤並將其修正。
  • 精簡程式碼,使其更有效率。

探索整數運算

開始使用一些以 C# 撰寫的基本整數運算。

  1. 在程式碼編輯器中,刪除預設 "Hello World" 程式碼。

    Screenshot that shows deleting the default Hello World code from your new calculator app.

    具體來說,刪除 Console.WriteLine("Hello World!"); 一行。

  2. 在其位置中,輸入下列程式代碼:

        int a = 42;
        int b = 119;
        int c = a + b;
        Console.WriteLine(c);
        Console.ReadKey();
    

    請注意,當您輸入程式代碼時,Visual Studio 中的 IntelliSense 功能會提供自動完成項目的選項。

    Animation of integer math code that shows the IntelliSense autocomplete feature in the Visual Studio IDE.

  3. 選取 [計算機] 旁的綠色 [開始] 按鈕,以建置並執行程式,或按 F5

    Screenshot that shows choosing the Calculator button to run the app from the toolbar.

    主控台視窗會隨即開啟並顯示 42 + 119 的總和,也就是 161

    Screenshot that shows a console window with the results of integer math.

  4. (選擇性),您可以變更運算子來變更結果。 例如,您可以將 int c = a + b;程式碼行中的 + 運算子變更為 - 進行相減、變更為 * 進行相乘,或變更為 / 進行相除。 在您執行程式時,結果也會變更。

  5. 關閉主控台視窗。

  1. 在 [方案總管] 的右窗格中,選取 [Program.cs] 以在程式碼編輯器中顯示檔案

  2. 在程式碼編輯器中,取代顯示 Console.WriteLine("Hello World!"); 的預設「Hello World」程式碼。

    Screenshot that shows the line to replace in the program file.

    將此行取代為下列程式碼:

        int a = 42;
        int b = 119;
        int c = a + b;
        Console.WriteLine(c);
        Console.ReadKey();
    

    如果您輸入程序代碼,Visual Studio IntelliSense 功能會提供自動完成項目的選項。

    Animation of integer math code that shows the IntelliSense autocomplete feature in the Visual Studio IDE.

  3. 若要建置並執行您的應用程式,請按 F5,或選取頂端工具列中 [計算機] 名稱旁邊的綠色箭號。

    Screenshot that showing selecting the Calculator button to run the app from the Debug toolbar.

    主控台視窗即會開啟,其中顯示 42 + 119 的總和,也就是 161

    Screenshot of a Console window showing the results of integer math.

  4. 關閉主控台視窗。

  5. 您可以選擇性地變更運算子來變更結果。 例如,您可以將 int c = a + b;程式碼行中的 + 運算子變更為 - 進行相減、變更為 * 進行相乘,或變更為 / 進行相除。 當您執行應用程式時,結果會相應地變更。

新增程式碼來建立計算機

繼續將一組更複雜的計算機程式碼新增至您的專案。

  1. 在程式碼編輯器中,將 Program.cs 中的所有程式碼取代為下列新程式碼:

        using System;
    
        namespace Calculator
        {
            class Program
            {
                static void Main(string[] args)
                {
                    // Declare variables and then initialize to zero.
                    int num1 = 0; int num2 = 0;
    
                    // Display title as the C# console calculator app.
                    Console.WriteLine("Console Calculator in C#\r");
                    Console.WriteLine("------------------------\n");
    
                    // Ask the user to type the first number.
                    Console.WriteLine("Type a number, and then press Enter");
                    num1 = Convert.ToInt32(Console.ReadLine());
    
                    // Ask the user to type the second number.
                    Console.WriteLine("Type another number, and then press Enter");
                    num2 = Convert.ToInt32(Console.ReadLine());
    
                    // Ask the user to choose an option.
                    Console.WriteLine("Choose an option from the following list:");
                    Console.WriteLine("\ta - Add");
                    Console.WriteLine("\ts - Subtract");
                    Console.WriteLine("\tm - Multiply");
                    Console.WriteLine("\td - Divide");
                    Console.Write("Your option? ");
    
                    // Use a switch statement to do the math.
                    switch (Console.ReadLine())
                    {
                        case "a":
                            Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
                            break;
                        case "s":
                            Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
                            break;
                        case "m":
                            Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
                            break;
                        case "d":
                            Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
                            break;
                    }
                    // Wait for the user to respond before closing.
                    Console.Write("Press any key to close the Calculator console app...");
                    Console.ReadKey();
                }
            }
        }
    
  2. 選取 [計算機] 按鈕或按 F5 來執行您的應用程式。

    主控台視窗隨即開啟。

  3. 在主控台視窗中,遵循提示將數字 42119 相加。

    您的應用程式看起來應該類似下列螢幕擷取畫面:

    Screenshot of a Console window showing the Calculator app with prompts.

  1. 在程式碼編輯器中,將 Program.cs 中的所有程式碼取代為下列新程式碼:

        // Declare variables and then initialize to zero.
        int num1 = 0; int num2 = 0;
    
        // Display title as the C# console calculator app.
        Console.WriteLine("Console Calculator in C#\r");
        Console.WriteLine("------------------------\n");
    
        // Ask the user to type the first number.
        Console.WriteLine("Type a number, and then press Enter");
        num1 = Convert.ToInt32(Console.ReadLine());
    
        // Ask the user to type the second number.
        Console.WriteLine("Type another number, and then press Enter");
        num2 = Convert.ToInt32(Console.ReadLine());
    
        // Ask the user to choose an option.
        Console.WriteLine("Choose an option from the following list:");
        Console.WriteLine("\ta - Add");
        Console.WriteLine("\ts - Subtract");
        Console.WriteLine("\tm - Multiply");
        Console.WriteLine("\td - Divide");
        Console.Write("Your option? ");
    
        // Use a switch statement to do the math.
        switch (Console.ReadLine())
        {
            case "a":
                Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
                break;
            case "s":
                Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
                break;
            case "m":
                Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
                break;
            case "d":
                Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
                break;
        }
        // Wait for the user to respond before closing.
        Console.Write("Press any key to close the Calculator console app...");
        Console.ReadKey();
    
  2. 選取 [計算機] 按鈕或按 F5 來執行您的應用程式。

    主控台視窗隨即開啟。

  3. 在主控台視窗中,遵循提示將數字 42119 相加。

    您的應用程式看起來應該類似下列螢幕擷取畫面:

    Screenshot of a Console window showing the Calculator app with prompts.

新增小數功能

現在,調校程式碼以新增更多功能。

目前計算機應用程式只接受並傳回整數。 例如,如果您執行應用程式,並將數字 42 除以數位 119,您的結果為零,這不是正確的。

Screenshot of a Console window that shows the Calculator app returning an inexact whole number as a result.

若要修正程式碼,藉由處理小數來改善精確度:

  1. 從 Visual Studio 編輯器中的 Program.cs,按 Ctrl+H 以開啟 [尋找和取代] 控制項。

  2. 在控制項中輸入 int,然後在 [取代] 欄位中輸入 float

  3. 選取控制項中代表 [大小寫須相符] 和 [全字拼寫須相符] 的圖示,或按 Alt+CAlt+W

  4. 選取 [全部取代] 圖示,或按 Alt+A 執行搜尋並取代。

    Animation of the Find and Replace control showing how to change the int variable to float.

  5. 再次執行您的計算機應用程式,將數字 42 除以數字 119

    應用程式現在會傳回小數,而不是零。

    Screenshot of a Console window showing the Calculator app that now returns a decimal numeral as a result.

    現在應用程式可以產生小數結果。 進一步調校程式碼,讓應用程式也可以計算小數。

  6. 使用 [尋找和取代] 控制項,將 float 變數的每個執行個體變更為 double,以及將 Convert.ToInt32 方法的每個執行個體變更為 Convert.ToDouble

  7. 執行您的計算機應用程式,並將數字 42.5 除以數字 119.75

    應用程式現在接受小數值,並會傳回較長的小數數字作為其結果。

    Screenshot of a Console window showing the Calculator app that now accepts decimal numbers and returns a longer decimal result.

    在 [修訂程式碼] 區段中,您可以減少結果中的小數位數。

偵錯應用程式

您改善了基本的計算機應用程式,但您的應用程式尚未處理例外狀況,例如使用者輸入錯誤。 例如,如果用戶嘗試除以零或輸入非預期的字元,應用程式可能會停止運作、傳回錯誤或傳回非數值非數值結果。

讓我們逐步解說一些常見的使用者輸入錯誤,在偵錯工具中將其找出 (如果這些錯誤出現在那裡),並在程式碼中修正。

提示

如需偵錯工具及其運作方式的詳細資訊,請參閱 Visual Studio 偵錯工具初探

修正「除以零」錯誤

如果您嘗試將數字除以零,主控台應用程式可能會凍結,然後向您顯示程式碼編輯器中出了什麼問題。

Screenshot of the Visual Studio code editor showing a line highlighted in yellow and an Exception Unhandled error for 'Attempted to divide by zero'.

注意

有時候應用程式不會凍結,而且偵錯工具不會顯示被零除錯誤。 相反地,應用程式可能會傳回非預期的非數值結果,例如無窮大符號。 下列程式碼修正仍然適用。

讓我們變更程式碼來處理此錯誤。 在 Program.cs 中,將 case "d": 的程式碼取代為下列程式碼:

            // Ask the user to enter a non-zero divisor until they do so.
                while (num2 == 0)
                {
                    Console.WriteLine("Enter a non-zero divisor: ");
                    num2 = Convert.ToInt32(Console.ReadLine());
                }
                Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
                break;
        }

在您取代程式碼之後,具有 switch 陳述式的區段應該看起來類似下列螢幕擷取畫面:

Screenshot showing the revised switch section in the Visual Studio code editor.

現在,當您將任何數字除以零時,應用程式會要求另一個數字,並持續要求,直到您提供非零的數字為止。

Screenshot of a Console window with a repeated prompt to provide a nonzero number.

修正「格式」錯誤

如果您在應用程式預期數字字元時輸入字母字元,應用程式會凍結。 Visual Studio 會向您顯示程式碼編輯器中出了什麼問題。

Screenshot showing an unhandled format error in the Visual Studio code editor.

Screenshot showing an unhandled format error in the Visual Studio code editor.

若要防止此例外狀況,您可以重構先前輸入的程式碼。

修改程式碼

您可以將應用程式分為兩個類別:CalculatorProgram,而不是依賴 program 類別來處理所有程式碼。

Calculator 類別會處理大量計算工作,而 Program 類別會處理使用者介面和錯誤處理工作。

這就開始吧。

  1. Program.cs 中,刪除所有項目並新增下列新的 Calculator 類別:

    class Calculator
    {
        public static double DoOperation(double num1, double num2, string op)
        {
            double result = double.NaN; // Default value is "not-a-number" if an operation, such as division, could result in an error.
    
            // Use a switch statement to do the math.
            switch (op)
            {
                case "a":
                    result = num1 + num2;
                    break;
                case "s":
                    result = num1 - num2;
                    break;
                case "m":
                    result = num1 * num2;
                    break;
                case "d":
                    // Ask the user to enter a non-zero divisor.
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    break;
                // Return text for an incorrect option entry.
                default:
                    break;
            }
            return result;
        }
    }
    
    
  2. 也會新增 Program 類別,如下所示:

    class Program
    {
        static void Main(string[] args)
        {
            bool endApp = false;
            // Display title as the C# console calculator app.
            Console.WriteLine("Console Calculator in C#\r");
            Console.WriteLine("------------------------\n");
    
            while (!endApp)
            {
                // Declare variables and set to empty.
                string numInput1 = "";
                string numInput2 = "";
                double result = 0;
    
                // Ask the user to type the first number.
                Console.Write("Type a number, and then press Enter: ");
                numInput1 = Console.ReadLine();
    
                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput1 = Console.ReadLine();
                }
    
                // Ask the user to type the second number.
                Console.Write("Type another number, and then press Enter: ");
                numInput2 = Console.ReadLine();
    
                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput2 = Console.ReadLine();
                }
    
                // Ask the user to choose an operator.
                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");
    
                string op = Console.ReadLine();
    
                try
                {
                    result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathematical error.\n");
                    }
                    else Console.WriteLine("Your result: {0:0.##}\n", result);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
                }
    
                Console.WriteLine("------------------------\n");
    
                // Wait for the user to respond before closing.
                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n") endApp = true;
    
                Console.WriteLine("\n"); // Friendly linespacing.
            }
            return;
        }
    }
    
  3. 選取 [計算機] 按鈕或按 F5 來執行您的應用程式。

  4. 遵循提示,將數字 42 除以數字 119。 您的結果看起來應該類似於下列螢幕擷取畫面:

    Screenshot showing a Console window with the refactored Calculator app.

    Screenshot showing a Console window with the refactored Calculator app.

    您現在可以執行更多計算,直到您選擇關閉主控台應用程式為止。 結果中的小數位數也較少。 如果您輸入不正確的字元,您會收到適當的錯誤回應。

關閉應用程式

  1. 如果您尚未這樣做,請關閉計算機應用程式。

  2. 關閉 Visual Studio 中的 [輸出] 窗格。

    Screenshot that shows closing the Output pane in Visual Studio.

  3. 在 Visual Studio 中,按 Ctrl+S 來儲存您的應用程式。

新增 Git 原始程式碼控制

既然您已建立應用程式,您可能就想要將其新增至 Git 存放庫。 Visual Studio 會透過您可以直接從 IDE 使用的 Git 工具,讓該流程變得容易。

提示

Git 是最廣泛使用的新式版本控制系統,因此無論您是專業開發人員,還是您正在學習如何撰寫程式碼,Git 都非常有用。 如果您不熟悉 Git,https://git-scm.com/ 網站是很好的起點。 在那裡,您可以在找到速查表、熱門的線上書籍和 Git 基本概念影片。

若要將程式碼與 Git 建立關聯,請先建立程式碼所在的新 Git 存放庫:

  1. 在 Visual Studio 右下角的狀態列中,選取 [新增至原始程式碼控制],然後選取 [Git]

    Screenshot of the Git source control buttons below the Solution Explorer pane, with the Add to Source Control button highlighted.

  2. 在 [建立 Git 存放庫] 對話方塊中,登入 GitHub。

    Screenshot of the Create a Git Repository dialog window where you can sign in to GitHub.

    存放庫名稱會根據您的資料夾位置自動填入。 您的新存放庫預設是私人的,這表示您是唯一可以存取此存放庫的人員。

    提示

    無論您的存放庫是公用還是私人,最好有程式碼的遠端備份安全地儲存在 GitHub 上。 即使您未與小組合作,遠端存放庫仍可讓您從任何電腦使用您的程式碼。

  3. 選取 [建立並推送]

    在建立您的存放庫之後,您會在狀態列中看到狀態詳細資料。

    Screenshot of the repo status bar that's below the Solution Explorer pane in Visual Studio.

    第一個具有箭號的圖示會顯示您的最新分支中有多少個傳出/傳入認可。 您可以使用此圖示來提取任何傳入認可或推送任何傳出認可。 您也可以選擇先檢視這些認可。 若要這樣做,請選取圖示,然後選取 [檢視傳出/傳入]

    具有鉛筆的第二個圖示會顯示程式碼未認可的變更數目。 您可以選取此圖示,在 [Git 變更] 視窗中檢視這些變更。

若要深入了解如何搭配您的應用程式使用 Git,請參閱 Visual Studio 版本控制文件

檢閱:程式碼完成

在本教學課程中,您已對計算機應用程式進行許多變更。 應用程式現在會更有效率處理運算資源,並處理大部分的使用者輸入錯誤。

以下是完整程式碼總整理:


class Calculator
{
    public static double DoOperation(double num1, double num2, string op)
    {
        double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.

        // Use a switch statement to do the math.
        switch (op)
        {
            case "a":
                result = num1 + num2;
                break;
            case "s":
                result = num1 - num2;
                break;
            case "m":
                result = num1 * num2;
                break;
            case "d":
                // Ask the user to enter a non-zero divisor.
                if (num2 != 0)
                {
                    result = num1 / num2;
                }
                break;
            // Return text for an incorrect option entry.
            default:
                break;
        }
        return result;
    }
}

class Program
{
    static void Main(string[] args)
    {
        bool endApp = false;
        // Display title as the C# console calculator app.
        Console.WriteLine("Console Calculator in C#\r");
        Console.WriteLine("------------------------\n");

        while (!endApp)
        {
            // Declare variables and set to empty.
            string numInput1 = "";
            string numInput2 = "";
            double result = 0;

            // Ask the user to type the first number.
            Console.Write("Type a number, and then press Enter: ");
            numInput1 = Console.ReadLine();

            double cleanNum1 = 0;
            while (!double.TryParse(numInput1, out cleanNum1))
            {
                Console.Write("This is not valid input. Please enter an integer value: ");
                numInput1 = Console.ReadLine();
            }

            // Ask the user to type the second number.
            Console.Write("Type another number, and then press Enter: ");
            numInput2 = Console.ReadLine();

            double cleanNum2 = 0;
            while (!double.TryParse(numInput2, out cleanNum2))
            {
                Console.Write("This is not valid input. Please enter an integer value: ");
                numInput2 = Console.ReadLine();
            }

            // Ask the user to choose an operator.
            Console.WriteLine("Choose an operator from the following list:");
            Console.WriteLine("\ta - Add");
            Console.WriteLine("\ts - Subtract");
            Console.WriteLine("\tm - Multiply");
            Console.WriteLine("\td - Divide");
            Console.Write("Your option? ");

            string op = Console.ReadLine();

            try
            {
                result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
                if (double.IsNaN(result))
                {
                    Console.WriteLine("This operation will result in a mathematical error.\n");
                }
                else Console.WriteLine("Your result: {0:0.##}\n", result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
            }

            Console.WriteLine("------------------------\n");

            // Wait for the user to respond before closing.
            Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
            if (Console.ReadLine() == "n") endApp = true;

            Console.WriteLine("\n"); // Friendly linespacing.
        }
        return;
    }
}

下一步

繼續進行本教學課程的第二個部分: