愛de告白網絡IT學習1群 215200319
I 分支語句↓↓↓↓↓↓
if語句(分支語句)
//分支語句之if語句
Console.WriteLine("輸入一個數值比較它與10的關系(整數)");
int a = int.Parse(Console.ReadLine());
//判斷變量a中存放數值與10的關系(整數)
if (a < 10)
{
Console.WriteLine(a + "小于10");
}
else if (a == 10)//此處可以使用if亦可以使用else if
{
Console.WriteLine(a + "等于10");
}
if (a > 10)
//或者最後一處用else
//else 不用括号與指派 因為除了小于等于就是剩下cw大于10
{
Console.WriteLine(a + "大于10");
}
Console.ReadKey();
switch語句(分支語句)
//分支語句switch
Console.WriteLine("請輸入1-7中的一個數字");
int week = int.Parse(Console.ReadLine());
switch (week)
{
case 1: Console.WriteLine("111111"); break;
case 2: Console.WriteLine("222222"); break;
case 3: Console.WriteLine("333333"); break;
case 4: Console.WriteLine("444444"); break;
case 5: Console.WriteLine("555555"); break;
case 6: Console.WriteLine("666666"); break;
case 7: Console.WriteLine("777777"); break;
default: Console.WriteLine("輸入錯誤"); break;
}Console.ReadKey();
?語句三位運算符(分支語句)
//分支語句之三位運算符
//提示使用者進行輸入
Console.WriteLine("請輸入要對比的數值,?問号的判定隻能判定2個數值");
int num = int.Parse(Console.ReadLine());
Console.WriteLine(num < 10 ? "您輸入的數值小于10" : "您輸入的數值大于等于10");
Console.ReadKey();
//?這個三位運算符隻能判斷2個結果
II 疊代語句↓↓↓↓↓↓
while語句(疊代語句)
// 疊代語句while語句
//輸出1-50到螢幕上
//while(條件表達式(bool類型傳回值))
int num = 1;
while(num<=50)//滿足條件後執行下面花括号
{
Console.WriteLine(num);
num++;//num自加1每次執行while循環
}
Console.ReadKey();
//利用while可以大量簡化重複執行的代碼
do...while語句(疊代語句)
//疊代語句之do...while語句
//輸出1-50到螢幕上
int num = 0;
do
{
num++;
Console.WriteLine(num);
} while (num < 50);//do花括号内隻要小于50便++
//此處規則(num!=0)便輸出不等于0的所有數值(int範圍内的數值)
Console.ReadKey();
//疊代語句之do...while語句使用計算銀行收益(本金存款*利率與存款升值)
double benjin = 0;
double lilv = 0;
int year = 0;
double mubiaoshouyi = 0;
Console.WriteLine("請輸入您的本金存款benjin");
benjin = double.Parse(Console.ReadLine());
Console.WriteLine("請輸入目前的利率lilv百分比");
lilv = double.Parse(Console.ReadLine())/100;
Console.WriteLine("請輸入您的目标收益");
do
{
mubiaoshouyi = double.Parse(Console.ReadLine());
if (mubiaoshouyi <= benjin) Console.WriteLine("您沒有必要存錢,請重新輸入一個比本金大的收益");
} while (mubiaoshouyi <=benjin);
//上面do内容為輸入值比benjin小時執行
do
{
benjin *= (lilv + 1);//本金*年份=年份(利率+本金*1)
year++;
} while (benjin < mubiaoshouyi);
Console.WriteLine("經過計算,您存款将在{0}年内,獲得{1}元的收益(本金+利息)",year,benjin);
Console.ReadKey();
for循環語句(疊代語句)
//for循環
//求輸入數的階乘
//1!=1 2!=2×1 3!=3×2×1
Console.WriteLine("請輸入需要計算階乘的數值");
long a = long.Parse(Console.ReadLine());
long jiecheng = 1;
for(long i=1;i<=a;i++)
{
jiecheng *= i;
}
Console.WriteLine("階乘的結果是"+jiecheng);
Console.ReadLine();
//for語句格式
//for(初始條件;判斷條件;循環條件)
//{
//循環語句;
//}
//for循環可以記錄循環的次數,有效控制循環的方式.
// 有時候,我們會省略初始條件,判斷條件,循環條件,(long i = 1; i <= a; i++)
//這時候的兩個分号是不能省略.此時構成一個死循環
//試了很多次,99乘法表vs2015依然沒有作出來,請大神指教,尤其是循環外自動排列那段該怎麼弄/????
//嵌套:就是套起來,将一個表格放在另一個表格中,稱為表格的嵌套.
//循環嵌套,也就是一個循環中包含了另外一個循環.
Console.WriteLine("==========九九乘法表==========");
//使用for循環時,一般在for循環語句進行聲明循環計次的變量.
{
for (int i = 1; i < 10; i++)//i為99乘法表第二個函數
for (int j = 1; j <= i; j++)
{
Console.Write("{0}×{1}={2} ", j, i, j * i);
Console.WriteLine();
}
for(int i=1; i<=2;)
for (int j = 2; j <= 2;)
Console.WriteLine("{0}×{1}={2} ", j, i, j * i);
Console.ReadKey();
}
foreach語句(疊代語句)
//将語句識别為單詞并逐行輸出
//語句用string類型,字母用char
//每執行一内含的代碼時,循環變量就會依次讀取集合中的一個元素.
//此處的循環變量隻是一個隻讀型的局部變量,這個值如果被修改編譯器
//會發生錯誤.
Console.WriteLine("輸入你要輸出的英文語句");
string str = Console.ReadLine();
foreach (char word in str)//雙TAB與CW類似
{
if(char.IsWhiteSpace(word))//如果是空格如何執行
{
Console.WriteLine();
}
else
{
Console.Write(word);//檢測到空格後換行
}
}
Console.ReadKey();
III 跳轉語句↓↓↓↓↓↓
//跳轉語句是程式運作到某一位置時,可以跳到程式中的另一行代碼的語句.
//跳轉語句主要有:break語句|switch語句中用到
// continue語句
// goto語句
// return語句
break語句(跳轉語句)
Console.WriteLine("請輸入1-7中的一個數字");
int week = int.Parse(Console.ReadLine());
switch (week)
{
case 1: Console.WriteLine("111111"); break;
case 2: Console.WriteLine("222222"); break;
case 3: Console.WriteLine("333333"); break;
case 4: Console.WriteLine("444444"); break;
case 5: Console.WriteLine("555555"); break;
case 6: Console.WriteLine("666666"); break;
case 7: Console.WriteLine("777777"); break;
default: Console.WriteLine("輸入錯誤"); break;
}
Console.ReadKey();
将程式設計看作是一門藝術,而不單單是個技術。
敲打的英文字元是我的黑白琴鍵,
思維圖紙畫出的是我編寫的五線譜。
當美妙的華章響起,現實通往二進制的大門即将被打開。