C#流程控制詳解_第1頁
C#流程控制詳解_第2頁
C#流程控制詳解_第3頁
C#流程控制詳解_第4頁
C#流程控制詳解_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第C#流程控制詳解//判斷2025年每個月的天數(shù),1,3,5,7,8,10,12為31天,4,6,9,11位30天,二月29天

Console.WriteLine("請輸月份數(shù)");

intmonth=int.Parse(Console.ReadLine());

switch(month)

case2:Console.WriteLine("您輸入的{0}月份有28天",month);break;

case4:

case6:

case9:

case11:

Console.WriteLine("您輸入的{0}月份有30天",month);break;

case1:

case3:

case5:

case7:

case8:

case10:

case12:

Console.WriteLine("您輸入的{0}月份有31天",month);break;

default:Console.WriteLine("您輸入的{0}月份錯誤",month);break;

Console.ReadKey();

}

3、三位運算符

條件判斷表達式?成立是執(zhí)行的語句:不成立時執(zhí)行的語句

三元運算符適用條件:只使用與判斷具有兩個結果的情況

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace三位運算符

classProgram

staticvoidMain(string[]args)

//判斷輸入述職與10的關系(10提示小于10,=10提示大于等于10)

Console.WriteLine("請輸入您要比較的數(shù)據(jù)");

intnumber=int.Parse(Console.ReadLine());

//Console.WriteLine(number10Console.WriteLine("小于10"):Console.WriteLine("大于等于10"));

Console.WriteLine(number10"小于10":"大于等于10");

Console.ReadKey();

}

4、迭代語句之while語句

4.1迭代語句概述

迭代語句時程序中重復的執(zhí)行,直到滿足指定以條件才停止的一段代碼。當用戶想重復執(zhí)行某些語句時,可依據(jù)當前不同的任務,

選擇不同的循環(huán)依據(jù)使用,分別是:

while語句dowhile語句for語句foreach語句

4.2while語句

while(條件表達式){

代碼語句

}

while語句當條件滿足時才執(zhí)行

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespacewhile語句

classProgram

staticvoidMain(string[]args)

//輸出1-50的數(shù)字到屏幕上

inta=1;

while(a=50){

Console.WriteLine(a);

a++;

Console.ReadKey();

}

5、迭代語句之dowhile

do{

循環(huán)體語句

}while();

dowhile語句至少執(zhí)行一次,即使條件不成立也會執(zhí)行一次

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespacedo__while

classProgram

staticvoidMain(string[]args)

//輸出1-50的數(shù)字到屏幕上

intnum=0;

do{

num++;

Console.WriteLine(num);

}while(num50);

//計算現(xiàn)金存入銀行多長時間才可以答案到我們的預期收益(均按一年期定期存款,到期后自動轉(zhuǎn)存)

//分析題目需要的變量:本金,目標收益,利率時間(年)

//一年的收益:本金*(1+利率)*1年

doubleBalace=0;

doubleRate=0;

intYear=0;

doubleTargetBalace=0;

Console.WriteLine("請輸入您的本金");

Balace=double.Parse(Console.ReadLine());

Console.WriteLine("請輸入您的當前利率百分比");

Rate=double.Parse(Console.ReadLine())/100;

Console.WriteLine("請輸入您的目標收益");

do{

TargetBalace=double.Parse(Console.ReadLine());

if(TargetBalaceBalace){

Console.WriteLine("恭喜您現(xiàn)在已經(jīng)擁有了{0}元,請輸入一個更大的目標",TargetBalace);

}while(TargetBalaceBalace);

Balace*=(Rate+1);

Year++;

}while(BalaceTargetBalace);

Console.WriteLine("您將在{0}年內(nèi),獲得{1}元的收益",Year,Balace);

Console.ReadKey();

}

6、迭代語句之for循環(huán)語句

for循環(huán)可以循環(huán)次數(shù)的限定,并維護自己的計時器;

有時候我們會省略初始條件,判斷條件,循環(huán)條件,但兩個分號不能省略

for(初始條件;判斷條件;循環(huán)條件){

循環(huán)語句

}

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespacefor循環(huán)語句

classProgram

staticvoidMain(string[]args)

//求輸入數(shù)據(jù)的階乘

//1!=12!=2x1;3!=3x2x1

Console.WriteLine("請輸入你要計算的階乘數(shù)");

for(;;){

intnum=int.Parse(Console.ReadLine());

intresult=1;

for(inti=num;i!=0;i--){

result*=i;

Console.WriteLine("{0}的階乘結果是{1}",num,result);

//Console.ReadKey();

}

for循環(huán)嵌套(九九乘法表)

循環(huán)嵌套就是一個循環(huán)中嵌套著另一個循環(huán)

使用for循環(huán)時,一般在for循環(huán)語句進行聲明循環(huán)計數(shù)次的變量

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespacefor循環(huán)語句

classProgram

staticvoidMain(string[]args)

//九九乘法表

Console.WriteLine("==========

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論