翻譯|使用教程|編輯:陳津勇|2019-11-19 11:50:27.097|閱讀 4207 次
概述:在本C#教程中,您將使用Visual Studio創(chuàng)建和運行控制臺應用程序,并在此過程中探索Visual Studio集成開發(fā)環(huán)境(IDE)的某些功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
建立項目
首先,我們將創(chuàng)建一個C#應用程序項目。在添加任何內(nèi)容之前,項目類型將隨您需要的所有模板文件一起提供。
1、打開Visual Studio 2019。
2、在開始窗口中,選擇創(chuàng)建一個新項目。

3、在“Create a new project”窗口上,在搜索框中輸入console。接下來,從“Language”列表中選擇C#,然后從“Platform”列表中選擇Windows。
應用語言和平臺過濾器后,選擇Console App(.NET Core)模板,然后選擇Next。

注意:如果看不到控制臺Console App (.NET Core)模板,則可以從“Create a new project”窗口中進行安裝。在“Not finding what you're looking for?”消息頁面,選擇安裝更多工具和功能鏈接。

然后在Visual Studio安裝程序中,選擇.NET Core跨平臺開發(fā)工作負載。

之后,在Visual Studio安裝程序中選擇“Modify”按鈕。系統(tǒng)可能會提示您保存工作,選擇保存就好了。接下來,選擇繼續(xù)安裝工作負載。然后,返回此“Create a project”過程中的步驟2 。
4、在“Configure your new project”窗口中,在“Project name”框中鍵入或輸入“Calculator”。然后,選擇Create。

Visual Studio將打開新項目,其中包括默認的“Hello World”代碼。
創(chuàng)建應用
首先,我們將探索C#中的一些基本整數(shù)數(shù)學。然后,添加代碼來創(chuàng)建基本計算器。之后,調(diào)試該應用程序,查找并修復錯誤。最后,為讓應用程序更高效,我們將優(yōu)化代碼。
探索整數(shù)數(shù)學
1、在代碼編輯器中,刪除默認的“Hello World”代碼。

具體來說,刪除表示的行Console.WriteLine("Hello World!");。
2、在Hello World的位置上,輸入以下代碼:
int a = 42; int b = 119; int c = a + b; Console.WriteLine(c); Console.ReadKey();
注意,在輸入時,Visual Studio中的IntelliSense功能為您提供了自動完成輸入的選項。

3、選擇Calculator運行程序,或按F5。

將打開一個控制臺窗口,其中顯示42 + 119的總和,即161。

4、(可選)您可以更改運算符來更改結果。例如,將代碼行中的+運算符更改為減、乘或除。然后,當運行程序時,結果也會改變。int c = a + b;-*/
5、關閉控制臺窗口。
添加代碼創(chuàng)建計算器
繼續(xù)向項目添加一組更復雜的計算器代碼。
1、刪除在代碼編輯器中看到的所有代碼。
2、輸入以下新代碼或?qū)⑵湔迟N到代碼編輯器中:
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();
        }
    }
}3、選擇計算器運行程序,或按F5。

將打開一個控制臺窗口。
4、在控制臺窗口中查看應用程序,然后按照提示添加數(shù)字42和119。

想要獲取 Visual Studio 更多資源或正版授權的伙伴請聯(lián)系領取
慧都16周年·技術服務月,軟件商城優(yōu)惠券不限量免費放送,購物立減服務升級,享受折上折>>>
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: