標籤:strong ar 2014 amp line new ad on
語句分類:
一.順序
對程式逐一執行
二.分支
if語句--用來判斷條件,bool型.
1.if() 2.if() 3.if() 4.if()
{ { { {
} } } }
else() else if() if()
{ { {
} } }
else if() else if()
{ {
} }
else() else()
PS:if()後只能有一個else(),但可有多個else if().
例1:求根
static void Main5(string[] args)
{
Console.WriteLine("求一元二次方程:a*x*x+bx+c=0根的情況");
Console.Write("a=");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("b=");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("c=");
int c = Convert.ToInt32(Console.ReadLine());
if (a == 0)
{
Console.WriteLine("不是一元二次方程");
}
else
{
Console.WriteLine("是一元二次方程");
int delta = b * b - 4 * a * c;
if (delta > 0)
{
Console.WriteLine("x1={0},x2={1}", (-b + Math.Sqrt(delta) / 2 * a), (-b - Math.Sqrt(delta) / 2 * a));--開方,求根結果.
}
else if (delta == 0)
{
Console.WriteLine("x=" + -b / 2 * a);
}
else
{
Console.WriteLine("無實根");
}
}
例2:猜拳
static void Main(string[] args)
{
Console.Write("請出拳:");
int human =Convert.ToInt32(Console.ReadLine());
Random mm = new Random();
int computer = mm.Next(3);
if (human == 0)
{
if (computer == 0)
{
Console.WriteLine("平局!");
}
if (computer == 1)
{
Console.WriteLine("你出剪刀,電腦出石頭,你輸了!");
}
if (computer == 2)
{
Console.WriteLine("你出剪刀,電腦出布,你贏了!");
}
else if (human == 1)
{
if (computer == 0)
{
Console.WriteLine("你出石頭,電腦出剪刀,你贏了!");
}
if (computer == 1)
{
Console.WriteLine("平局!");
}
if (computer == 2)
{
Console.WriteLine("你出石頭,電腦出布,你輸了!");
}
else if (human == 2)
{
if (computer == 0)
{
Console.WriteLine("你出布,電腦出剪刀,你輸了!");
}
if (computer == 1)
{
Console.WriteLine("你出布,電腦出石頭,你贏了!");
}
if (computer == 2)
{
Console.WriteLine("平局!");
}
else
{
Console.WriteLine("請按套路出拳!謝謝!");
}
}
}
PS:產生隨機產生器:
Random sss = new Random();
int 變數=sss.next();
Console.Writeline(變數);
例3:標準體重
static void Main8(string[] args)
{
Console.Write("性別:");
string sex = Console.ReadLine();
Console.Write("身高:");
int h = Convert.ToInt32(Console.ReadLine());
Console.Write("體重:");
int w = Convert.ToInt32(Console.ReadLine());
if (sex == "男")
{
int w1 = h - 100 - 3;
int w2 = h - 100 + 3;
if (w <= w2 && w >= w1)
{
Console.WriteLine("體重正常!");
}
else if (w > w2)
{
Console.WriteLine("體重偏胖哦,需要鍛煉啦!");
}
else if (w < w1)
{
Console.WriteLine("體重偏瘦,要注意營養搭配哦!");
}
else
{
Console.WriteLine("親有體重?");
}
}
else
{
int w1 = h - 100 - 3;
int w2 = h - 100 + 3;
if (w <= w2 && w >= w1)
{
Console.WriteLine("體重正常!");
}
else if (w > w2)
{
Console.WriteLine("體重偏胖哦,需要鍛煉啦!");
}
else if (w < w1)
{
Console.WriteLine("體重偏瘦,要注意營養搭配哦!");
}
else
{
Console.WriteLine("親有體重?");
}
}
PS:斷點--檢查程式錯誤
工具--選項--文字編輯器--所有語言--常規--行號(點選).--(CTRL+F5鍵不執行該程式,只能啟動)
調試--啟動調試--視窗--即時--逐語句.--看程式運行步驟,如執行“if”,即不執行“else”.