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