程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 總結:C#變量,占位符等相關知識,

總結:C#變量,占位符等相關知識,

編輯:C#入門知識

總結:C#變量,占位符等相關知識,


新年耽誤了不少時間,好久沒認真的坐下來學習了,新年也快完了,又要開始正式學習了,按著視頻教學學習,用了一天的時間,學習了下簡單的變量及其相關的輸入輸出和應用,學了幾種最基本的類型:

int(整型) char(字符型) string(字符串類型)double(雙精度浮點數) decimal(貨幣值類型) float(浮點數)。

Main方法中,不允許重復申明變量,但可以重復賦值,重復賦值以後原來的變量值被頂替為新賦的值。

一.  在C#中,“+” 有兩種含義;

1.聯值符號,當+左右兩邊只要有一邊是字符或者字符串類型的時候,用“+”表示連接左右兩邊的數據。

2.數學中的加號,參與運算的是字符型的數據,表示進行數學上的加法運算。

賦值運算符=(不是數學中的等於符號),是C#中最低的運算等級,在最後執行。

二. 占位符

第一個{0}

第二個{1}

第三個{2}

.......

例如:Console.WriteLine("姓名{0} 性別{1} 年齡{2}",name,sex,age);

復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 輸出變量與聯值
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            name = "張三";
            int age = 28;
            age = 18; //重復賦值變量age的值。
            decimal pay = 7600.33m;

            //Console.Write("我叫"+name);
            //Console.Write(",今年"+age+"歲,");
            //Console.Write("我的工資是"+pay+"元.");

            //Console.WriteLine("我叫"+name+",今年"+age+"歲,"+"我的工資是"+pay+"元.");            
            Console.WriteLine("我叫{0},今年{1}歲,我的工資是{2}元.", name, age, pay);//{0}{1}{2}表示占位符。占位符可以重復使用,可以省略。
           
            Console.WriteLine("我叫"+name,"今年"+age+"歲了.");//逗號前為第一個參數,console輸出逗號前的第一個參數。
            Console.WriteLine("{0}我叫" + name, "今年" + age + "歲了.");//{0}"今年" + age + "歲了."代替前面的占位符的變量。


           
            
            int a = 1;//同為數字類型的用“+”表示數學上的加法。
            //string a = "1";  聯值符號的用法區別,左右兩邊只要一邊有字符或者字符串類型用“+”就是聯值符號。  
            int b = 2;
            Console.WriteLine(a+b);
            Console.WriteLine("1+2");
            Console.ReadKey();
        }
    }
}
復制代碼 復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 變量作業
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "張三";
            string Tel = "13111111111";
            char sex = '男';
            int age = 25;
            Console.WriteLine("{0},{1},{2},{3}",name,Tel,sex,age);
            Console.ReadKey();


        }
    }
}
復制代碼 復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 變量作業4
{
    class Program
    {
        static void Main(string[] args)
        {
            string Pho = "SAMSUNG";
            string type = "I9300";
            decimal money = 3799m;
            string weight = "0.3kg";//double weight = 0.3;
            Console.WriteLine("我的手機牌子是{0},型號是{1},手機價格是{2}元,重量是{3}",Pho,type,money,weight);

            Console.ReadKey();
        }
    }
}
復制代碼

 

Console.ReadLine();用於接收用戶輸入的數據,需要定義一個字符串類型(string)的變量來存儲用戶的變量。

  string input;

  input=Console.ReadLine(); 

等價於 string input=Console.ReadLine();

復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 用戶輸入
{
    class Program
    {
        static void Main(string[] args)
        {
           // string input;
            Console.WriteLine("輸入這句話的前面");
            Console.WriteLine("請在這裡輸入一句話!");
            string input = Console.ReadLine();
            Console.WriteLine("輸入這句話的後面");
            Console.ReadKey();

        }
    }
}
復制代碼

三. 交換變量數值

若要相互交換兩個變量的數值,需要借助第三個變量來完成。

  int a =5,b=10;

  int c;

  c = b;

  b = a;

  a = c;

  Console.WriteLine("a={0} b={1}",a,b);

  Console.ReadKey();

復制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 交換變量
{
    class Program
    {
        static void Main(string[] args)
        {   //交換兩個變量的算法,需要介助第三個變量。
            int a = 5;
            int b = 10;
            int c;
            c = a;
            a = b;
            b = c;

            Console.WriteLine("a={0} b={1}",a,b);

            Console.WriteLine("a={0} b={1}",b,a);//並不會交換兩個變量的值
            Console.ReadKey();
        }
    }
}
復制代碼

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved