網(wǎng)站首頁 編程語言 正文
流程控制語句分類
- 分支語句: if語句和switch語句
- 迭代語句
- 跳轉(zhuǎn)語句
1、if語句
if (判斷條件表達(dá)式){ 表達(dá)式結(jié)果為true時執(zhí)行}else{表達(dá)式結(jié)果為false時執(zhí)行}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace if語句
{
class Program
{
static void Main(string[] args)
{
//判斷a變量與10的關(guān)系
Console.WriteLine("請輸入你要比較的第一個數(shù)字");
int a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入你要比較的第而個數(shù)字");
//int.parse 用于將屏幕輸入的語句轉(zhuǎn)換為整型
int b = int.Parse(Console.ReadLine());
if (a < b)
{
Console.WriteLine("您輸入的第一個數(shù)字{0}小于第二個數(shù)字{1}", a,b);
}
else if (a == b)
{
Console.WriteLine("您輸入的第一個數(shù)字{0}等于第二個數(shù)字{1}", a,b);
}
else {
Console.WriteLine("您輸入的第一個數(shù)字{0}大于第二個數(shù)字{1}", a,b);
}
Console.ReadKey();
}
}
}
2、switch
輸入1顯示為星期一,依次類推
swithc(條件表達(dá)式){
case 常量表達(dá)式:條件語句;
case 常量表達(dá)式:條件語句;
case 常量表達(dá)式:條件語句;
default:條件表達(dá)式
}
控件無法從最終用例標(biāo)簽(XX)脫離開關(guān)——程序無法判定為結(jié)束,所以必須加一個break;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace switch控制語句
{
class Program
{
static void Main(string[] args)
{
// 輸入一顯示星期一,一次類推
Console.WriteLine("請輸入1-7的數(shù)字");
int week = int.Parse(Console.ReadLine());
switch (week) {
case 1: Console.WriteLine("星期一"); break; //結(jié)束當(dāng)前代碼體
case 2: Console.WriteLine("星期二"); break;
case 3: Console.WriteLine("星期三"); break;
case 4: Console.WriteLine("星期四"); break;
case 5: Console.WriteLine("星期五"); break;
case 6: Console.WriteLine("星期六"); break;
case 7: Console.WriteLine("星期日"); break;
default: Console.WriteLine("您輸入的數(shù)據(jù)錯誤"); break; //超出規(guī)定值設(shè)置相應(yīng)提示
}
Console.ReadKey();
//判斷2020年每個月的天數(shù), 1,3,5,7,8,10,12為31天,4,6,9,11位30天,二月29天
Console.WriteLine("請輸月份數(shù)");
int month = int.Parse(Console.ReadLine());
switch (month)
{
case 2: Console.WriteLine("您輸入的{0}月份有28天",month); break;
case 4:
case 6:
case 9:
case 11:
Console.WriteLine("您輸入的{0}月份有30天",month); break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("您輸入的{0}月份有31天", month); break;
default: Console.WriteLine("您輸入的{0}月份錯誤", month); break;
}
Console.ReadKey();
}
}
}
3、三位運算符
條件判斷表達(dá)式?成立是執(zhí)行的語句:不成立時執(zhí)行的語句
三元運算符適用條件:只使用與判斷具有兩個結(jié)果的情況
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 三位運算符
{
class Program
{
static void Main(string[] args)
{
// 判斷輸入述職與10的關(guān)系(<10 提示小于10, >=10提示大于等于10)
Console.WriteLine("請輸入您要比較的數(shù)據(jù)");
int number = int.Parse(Console.ReadLine());
//Console.WriteLine(number < 10 ? Console.WriteLine("小于10") : Console.WriteLine("大于等于10") );
Console.WriteLine(number < 10 ? "小于10" : "大于等于10");
Console.ReadKey();
}
}
}
4、迭代語句之while語句
4.1 迭代語句概述
迭代語句時程序中重復(fù)的執(zhí)行,直到滿足指定以條件才停止的一段代碼。當(dāng)用戶想重復(fù)執(zhí)行某些語句時,可依據(jù)當(dāng)前不同的任務(wù),
選擇不同的循環(huán)依據(jù)使用,分別是:
- while語句
- do……while語句
- for語句
- foreach語句
4.2 while語句
while(條件表達(dá)式){
代碼語句
}
while語句當(dāng)條件滿足時才執(zhí)行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace while語句
{
class Program
{
static void Main(string[] args)
{
//輸出1-50的數(shù)字到屏幕上
int a = 1;
while (a<=50){
Console.WriteLine(a);
a++;
}
Console.ReadKey();
}
}
}
5、迭代語句之do……while
do{
循環(huán)體語句
}while();
do……while語句至少執(zhí)行一次,即使條件不成立也會執(zhí)行一次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace do__while
{
class Program
{
static void Main(string[] args)
{
//輸出1-50的數(shù)字到屏幕上
int num = 0;
do {
num++;
Console.WriteLine(num);
} while (num < 50);
// 計算現(xiàn)金存入銀行多長時間才可以答案到我們的預(yù)期收益(均按一年期定期存款,到期后自動轉(zhuǎn)存)
// 分析題目需要的變量 :本金, 目標(biāo)收益,利率 時間(年)
// 一年的收益: 本金*(1+利率)*1年
double Balace = 0;
double Rate = 0;
int Year = 0;
double TargetBalace = 0;
Console.WriteLine("請輸入您的本金");
Balace = double.Parse(Console.ReadLine());
Console.WriteLine("請輸入您的當(dāng)前利率百分比");
Rate = double.Parse(Console.ReadLine())/100;
Console.WriteLine("請輸入您的目標(biāo)收益");
do {
TargetBalace = double.Parse(Console.ReadLine());
if (TargetBalace<Balace) {
Console.WriteLine("恭喜您現(xiàn)在已經(jīng)擁有了{(lán)0}元,請輸入一個更大的目標(biāo)",TargetBalace);
}
} while (TargetBalace<Balace);
do
{
Balace *= (Rate + 1);
Year++;
} while (Balace < TargetBalace);
Console.WriteLine("您將在{0}年內(nèi),獲得{1}元的收益",Year,Balace);
Console.ReadKey();
}
}
}
6、迭代語句之for循環(huán)語句
for循環(huán)可以循環(huán)次數(shù)的限定,并維護(hù)自己的計時器;
有時候我們會省略初始條件,判斷條件,循環(huán)條件,但兩個分號不能省略
for(初始條件;判斷條件;循環(huán)條件){
循環(huán)語句
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace for循環(huán)語句
{
class Program
{
static void Main(string[] args)
{
//求輸入數(shù)據(jù)的階乘
// 1!=1 2!=2x1; 3!=3x2x1
Console.WriteLine("請輸入你要計算的階乘數(shù)");
for (;;) {
int num = int.Parse(Console.ReadLine());
int result = 1;
for (int i=num; i!=0; i--) {
result *= i;
};
Console.WriteLine("{0}的階乘結(jié)果是{1}", num, result);
};
//Console.ReadKey();
}
}
}
for循環(huán)嵌套(九九乘法表)
循環(huán)嵌套就是一個循環(huán)中嵌套著另一個循環(huán)
使用for循環(huán)時,一般在for循環(huán)語句進(jìn)行聲明循環(huán)計數(shù)次的變量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace for循環(huán)語句
{
class Program
{
static void Main(string[] args)
{
//九九乘法表
Console.WriteLine("==================九九乘法口訣=========================");
for (int i = 1; i < 10; i++) {
for (int j=1; j<= i; j++) {
Console.Write("{0}X{1}={2}\t", j, i, j * i);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
7、迭代語句之foreach
foreach提供了一個for語句的捷徑,而且還存進(jìn)了集合類更為一致
foreach(類型;變量;in 集合){
代碼體
}
string類型(字符串)可以看成是char類型(字符)的一個集合
char.IsWhiteSpace? 判斷字符是不是空格
foreach每執(zhí)行一內(nèi)含代碼,循環(huán)變量就會一次讀取集合中的一個元素,向當(dāng)時循環(huán)便利
此處循環(huán)變量只是一個只讀型的局部變量,這個值如果被修改編譯會報錯
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @foreach
{
class Program
{
static void Main(string[] args)
{
//將語句識別為單詞,并逐行輸出
//語句用string類型,字母用char
Console.WriteLine("請輸入一句英文語句");
string sentence = Console.ReadLine();
foreach (char word in sentence)
{
if (char.IsWhiteSpace(word))
{
Console.WriteLine();
}
else
{
Console.Write(word);
//word='t'; //foreach語句的迭代變量不允許重新賦值
}
}
Console.ReadLine();
}
}
}
8、跳轉(zhuǎn)語句之break語句
跳轉(zhuǎn)語句是程序運行到某一位置時,可以跳轉(zhuǎn)到程序中另一行代碼的語句
- break:1)switch語句中用于從case語句中跳出,結(jié)束switch分支語句。2)用于跳出迭代語句結(jié)束當(dāng)前訓(xùn)話
- continute語句
- goto語句
- return語句
通過迭代語句,準(zhǔn)備輸出1~500這500個數(shù),每行輸出10個數(shù)。當(dāng)輸出的值同時是2、3、4、5、6】7的倍數(shù)是,跳出for迭代語句。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace break語句
{
class Program
{
static void Main(string[] args)
{
//通過迭代語句,準(zhǔn)備輸出1~500這500個數(shù),每行輸出10個數(shù)。當(dāng)輸出的值同時是2、3、4、5、6、7的倍數(shù)是,跳出for迭代語句。
Console.WriteLine("輸出1~500這500個數(shù),每行輸出10個數(shù)");
for (int i=1;i<501;i++) {
if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) {
Console.WriteLine();
Console.WriteLine("2、3、4、5、6、7的最小公倍數(shù)倍數(shù)是"+i);
break;
}
if (i % 10 == 0)
{
Console.WriteLine(i);
}
else Console.Write(i + "\t");
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace break語句
{
class Program
{
static void Main(string[] args)
{
//通過迭代語句,準(zhǔn)備輸出1~500這500個數(shù),每行輸出10個數(shù)。當(dāng)輸出的值同時是2、3、4、5、6、7的倍數(shù)是,跳出for迭代語句。
Console.WriteLine("輸出1~500這500個數(shù),每行輸出10個數(shù)");
for (int i=1;i<501;i++) {
if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) break;
//{
// Console.WriteLine();
// Console.WriteLine("2、3、4、5、6、7的最小公倍數(shù)倍數(shù)是"+i);
// break;
//}
if (i % 10 == 0)
{
Console.WriteLine(i);
}
else Console.Write(i + "\t");
}
Console.ReadKey();
}
}
}
9、continue語句
用于停止當(dāng)前的迭代語句,結(jié)束本次循環(huán),進(jìn)入下一次循環(huán)(本次循環(huán)中continue后面的語句不執(zhí)行)。breack是直接結(jié)束循環(huán)
只能用于迭代語句中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace continute語句
{
class Program
{
static void Main(string[] args)
{
//實現(xiàn)50以內(nèi)的奇數(shù)輸出,利用continue
Console.WriteLine("請輸入一個數(shù),會自動顯示小于次數(shù)的所有奇數(shù)");
int num = int.Parse(Console.ReadLine());
for (int i = 1; i < num+1; i++)
{
if (i % 2 == 0) continue; //滿足條件時跳出此次循環(huán),進(jìn)入下一個循環(huán);且本次循環(huán)continute后的語句不執(zhí)行
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
10、跳轉(zhuǎn)語句之return
return語句使用時,一般有兩種格式:1)return; 2)return 表達(dá)式;
return語句只能出現(xiàn)在方法當(dāng)中,當(dāng)調(diào)傭方法時,執(zhí)行到return語句時;直接跳轉(zhuǎn)到main()函數(shù)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace continute語句
{
class Program
{
static void Main(string[] args)
{
//實現(xiàn)50以內(nèi)的奇數(shù)輸出,利用continue
Console.WriteLine("請輸入一個數(shù),會自動顯示小于次數(shù)的所有奇數(shù)");
int num = int.Parse(Console.ReadLine());
for (int i = 1; i < num+1; i++)
{
if (i % 2 == 0) continue; //滿足條件時跳出此次循環(huán),進(jìn)入下一個循環(huán);且本次循環(huán)continute后的語句不執(zhí)行
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
使用方法實現(xiàn):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @return
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入三個整數(shù),按回車鍵確認(rèn)每個數(shù)的輸入");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
//double averageresult = (a + b + c) / 3;
double averageresult = average(a,b,c);
Console.WriteLine("您輸入的三個數(shù){0}、{1}、{2}的平均數(shù)是{3}",a,b,c, averageresult);
Console.ReadKey();
}
static double average(int a, int b, int c) {
return (a + b + c) / 3;
}
}
}
11、跳轉(zhuǎn)語句之goto
格式:goto 標(biāo)標(biāo)識符;
標(biāo)識符標(biāo)識程序位置的方法
標(biāo)識方法——標(biāo)識符+“:”
作用:當(dāng)程序執(zhí)行到goto語句時,程序會直接跳轉(zhuǎn)到標(biāo)識符所表示的程序位置。繼續(xù)執(zhí)行
goto的使用會使代碼的易讀性下降,在編寫程序的時候盡量少用goto語句
任務(wù):利用goto語句實現(xiàn)選擇題:
5!=?
1、5!=5
2、5!=10
3、5!=20
4、5!=60
如果選擇真確,提示:恭喜你,答對了!
如果選擇錯誤,提示:很遺憾,你答錯了
如果選擇的選項不是1、2、3、4,提示:你所選的選項不存在
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace goto語句
{
class Program
{
static void Main(string[] args)
{
int a = 0;
Console.WriteLine("請選擇5的階乘正確答案,輸入選項編號回車鍵確認(rèn)");
Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60\n");
error:
{
a++; //第一次執(zhí)行時 a=1;因此不執(zhí)行,當(dāng)goto跳轉(zhuǎn)到此語句時,再次自加1,a=2此時執(zhí)行下面語句
if (a > 1) Console.WriteLine("很遺憾,您打錯了,請重新輸入答案"); // 加入a判斷條件原因是,避免在第一次執(zhí)行是輸出此提示
}
input: int result = int.Parse(Console.ReadLine());
switch (result) {
case 1:
case 2:
case 3: goto error;
case 4: goto right;
default:
Console.WriteLine("您的選項{0}不存在,請重新輸入",result);
goto input;
}
right:
{
Console.WriteLine("恭喜你答對了!");
}
Console.ReadKey();
}
}
}
12、任務(wù)實施
接受a\b\c三個整數(shù),然后輸出三個數(shù)中居中的那個數(shù),并輸出其階乘
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 任務(wù)實施
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入三個整數(shù)");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
//判斷中間變量
///如果a是中間值,那么有兩種情況,b是最大值或b是最小值
int temp = 0;
int jc = 1;
if ((a>=b && a<=c) || (a>=c && a<=b)) {
Console.WriteLine(a + "是中間值");
temp = a;
Console.WriteLine("錯誤");
}
if (b >= a && b <= c || b >= c && b <= a)
{
Console.WriteLine(b + "是中間值");
temp = b;
}
if (c >= a && c <= b || c >= b && c <= a)
{
Console.WriteLine(c + "是中間值");
temp = c;
}
for (int i = 1; i < b+1; i++)
{
jc *= i;
}
Console.WriteLine("中間數(shù){0}階乘結(jié)果是{1}",temp,jc);
Console.ReadKey();
}
}
}
原文鏈接:https://blog.csdn.net/Mwyldnje2003/article/details/122511709
相關(guān)推薦
- 2022-03-27 關(guān)于Android?Device?Monitor?無法打開問題_Android
- 2022-12-10 Redis數(shù)據(jù)庫安全詳解_Redis
- 2022-10-14 yum 倉庫管理 yum-config-manager
- 2022-08-15 ArrayList和LinkedList和Vector的區(qū)別
- 2022-03-20 詳解C語言對字符串處理函數(shù)的實現(xiàn)方法_C 語言
- 2022-04-09 如何利用python提取字符串中的數(shù)字_python
- 2022-09-14 C++中地圖按鍵排序?qū)崿F(xiàn)示例_C 語言
- 2023-04-26 C語言rand和srand函數(shù)使用方法介紹_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支