【2017-2-17】C#基礎-定義變數,輸入輸出

來源:互聯網
上載者:User

標籤:nihao   單詞   line   方案   資源管理員   主題   資料   space   多少   

主要學習內容為:

控制台程式的建立,輸出,輸入,定義變數,變數賦值,值覆蓋,值拼接,值列印
兩種資料類型,整數型別轉換

程式設計語言檔案名稱尾碼為.cs

 

建立程式項目的步驟:

檔案——建立——項目——Visual C#——.Net Framework 4——控制台應用程式——設定名稱和位置——確定

 

方案總管可以設定編寫或啟動某個項目

 

main函數中編寫代碼

 

列印內容:

//往外輸出內容的
Console.WriteLine("輸出的內容");    換行
Console.Write("輸出的內容");         不換行

 

接收使用者輸入的內容:

//等待使用者輸入,按斷行符號鍵結束,防止程式閃退
Console.ReadLine(); - 會返回一個string(字串)類型的值

(注意寫代碼時單字首大寫,標點符號不要遺漏及英文格式)

 

工具——選項——文字編輯器——C#——顯示——行號        查看程式碼數

工具——選項——環境——常規——顏色主題                    可設定程式背景

工具——選項——環境——字型和顏色——Consolas          設定寫代碼專用字型

 

定義變數:(必須字母開頭,不要帶有特殊符號,盡量不要用中文,必須要定義後賦值,不能重複定義變數,可以一次性定義多個變數,中間用逗號分隔)
string a;  或  string a ="值"

變數賦值:(變數必須在左側)
a = "值";

列印變數:
Console.WriteLine(變數名);
Console.Write(變數名);

兩個string類型進行“+”操作,結果是:拼接

兩個string類型無法相減

1 Console.WriteLine(“請輸入您的姓名”);2 string name =Console.ReadLine();3 string nihao=“你好,”;4 string end =nihao +name;5 Console.WriteLine(end);6 Console.ReadLine();

 

定義整型變數:
int a;    或 int a =數字
整型變數賦值:
a = 數字;

整型變數可以執行“+-*/”操作,結果是數學運算

如何把字串類型轉換成整型?
string a = "數字";
int b = int.Parse(a);

1 string a =Console.ReadLine();2 int b =int.Parse(a);3 Console.WriteLine(b);4 Console.ReadLine();

 

自選題目

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 練習題3{    class Program    {        static void Main(string[] args)        {            Console.Write("您的母親是几几年生人?");            string a=Console.ReadLine();            Console.Write("您今年多少歲?");            string b=Console.ReadLine();            int c = 2017;                int d=int.Parse(a);            int e=int.Parse(b);            int F=c-d-e;            Console.Write("您誕生的時候,您母親"+F+"歲");            Console.ReadLine();        }    }}

 

【2017-2-17】C#基礎-定義變數,輸入輸出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.