跟著視頻學 c# asp.net 第二天,
課程要點:
把變量看成存放數據的容器
定義變量的方式:類型 變量名; int i3;變量只是容器,必須放進去值才有意義,否則就沒有意義.
int i2=5;
變量的類型:不同類型的容器放不同的東西。不能在int類型的變量中放字符串。
變量不能放和變量類型不兼容的數據。
string、int 、char 、bool long等。bool的取值:true、false。int的表示范圍。long有多long
為什麼輸出"要用轉義符"\"",因為編譯器默認是遇到"開始字符串,再遇到"是結束字符串,但是如果遇到前面有\的"就不把它當成有字符串起始意義的"。\表示不要把\後的"當成字符串的開始或者結尾
為什麼要有轉義符,就是要在程序中輸出回車等特殊的字符,不能直接在字符串中打回車,所以必須轉移。"\n"回車。string:"\"ab\""、"ab\nb"、"c:\\a.txt"、@"c:\a.txt"(推薦)。@表示字符串中的\不當成轉義符。@還可以定義多行文本。"\\\\"得到的是兩個\
"\""中\是告訴編譯器不要把這個"當成字符串的結束。
@是不把\當成轉義符。@不是萬能的,不能解決字符串中有雙引號的問題,如果有雙引號還是用轉義符
簡單的類型轉換:Convert.ToString()、ToString()、Convert.ToInt32() 。即可用中間變量,也可以不用。int i = Convert.ToInt32(Console.ReadLine());
變量的命名規則:
第一個字符必須是字母或者下劃線(_),其後的字符可以是任意個數字、字母、下劃線。不能全部使用C#的關鍵字,比如class、namespace、new、void等。判斷方式:VS中亮藍色的就是關鍵字
•相等判斷:==,不要和=混淆。WriteLine("{0}",i==1);WriteLine("{0}",i=1);的區別。Console.WriteLine("{0}",i=1);//C#中賦值表達式也有值,它的值表示為賦值後變量的值
•不等判斷:!=
•大小比較:<、>、<=、>=
•取反:!
•組合運算:&&(並且)、||(或者)。
–&& 並且:只有兩邊都為true的時候,表達式的值才為true,否則是false;
–||或者:兩邊只要有一個為true的時候,表達式的值就是true,否則是false;
程序代碼:
變量:

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 變量
{
class Program
{
static void Main(string[] args)
{
int a = 10;
a = 11;
Console.WriteLine(a);
string s = "朋友,你好!";
Console.WriteLine(s);
char c = 'a';
Console.WriteLine(c);
char c1 = '白';//在c# 裡面漢字也表示一個字符
Console.WriteLine(c1);
bool b = true;
Console.WriteLine(b);//只有兩個取值 true ,false
//int 的最大值,最小值
Console.WriteLine("int的最大值{0},最小值{1}",int.MaxValue,int.MinValue);
Console.ReadKey();
}
}
}
View Code
字符串

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串1
{
class Program
{
static void Main(string[] args)
{
//string name = "to\"m";//如果要輸出 to"m 則需要用到轉義字符 \表示不能把\ 後面的"看成開始或結束
//string name = "to\nm";
string name = "to\\m";
//string s = "C:\\Intel\\Logs";
string s = @"C:\Intel\Logs";
string s1 = @"sjdfjeojfla
fpelpflep";//加上@聲明多行字符串
Console.WriteLine(name);
Console.WriteLine(s);
Console.WriteLine(s1);
//數據類型轉換
string s2 = "2345";
int a = Convert.ToInt32(s2);
int b = a + 20;
Console.WriteLine(b);
Console.ReadKey();
}
}
}
View Code
數據類型轉換

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 數據類型轉換
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入第一個數字:");
string num1 = Console.ReadLine();
Console.WriteLine("請輸入第二個數:");
string num2 = Console.ReadLine();
//Console.WriteLine(num1+num2);//這裡是把兩個字符串連接起來
int i1 = Convert.ToInt32(num1);
int i2 = Convert.ToInt32(num2);
Console.WriteLine(i1+i2);//轉換為int類型
Console.ReadKey();
}
}
}
View Code
交換兩個變量的值

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 交換兩個變量的值
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
Console.WriteLine("a的值是{0},b的值是{1}", a, b);
int temp = a;
a = b;
b = temp;
Console.WriteLine("a的值是{0},b的值是{1}",a,b);
Console.ReadKey();
}
}
}
View Code
布爾運算

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 布爾運算
{
class Program
{
static void Main(string[] args)
{
int i = 5;
bool b=(i==9);//判斷i是否等於9,如果等於9則為true,否則為false
b = (i != 9);//不等於
b=(i>=9);//大於等於
bool a = true;
a = !a;
Console.WriteLine(b);
Console.WriteLine(a);
int i1 = 9;
int i2 = -9;
bool bool_1 = (i1 > 0 && i2 > 0);
bool bool_2 = (i1 > 0 || i2 > 0);
Console.WriteLine(bool_1);
Console.WriteLine(bool_2);
Console.ReadKey();
}
}
}
View Code