首页 > 编程语言 >C#学习笔记--逻辑语句(分支和循环)

C#学习笔记--逻辑语句(分支和循环)

时间:2023-10-10 16:56:04浏览次数:30  
标签:语句 10 Console C# break -- int WriteLine case

逻辑语句

条件分支语句

条件分支语句可以让顺序执行的代码逻辑产生分支,满足对应条件地执行对应代码逻辑。
IF语句

//IF语句块
int a=5;
if(a>0&&a<15)//注意结尾无分号
{
    Console.WriteLine("a在0到15之间");
}
//if……else结构
if( false )
{
    Console.WriteLine("满足if条件 做什么");
    if( true )
    {
        if (true)
        {

        }
        else
        {

        }
    }
    else
    {
        if (true)
        {

        }
        else
        {

        }
    }
}
else
{
    Console.WriteLine("不满足if条件 做什么");
    if (true)
    {

    }
    else
    {

    }
}
//if……elseif 结构
int a3 = 6;
if (a3 >= 10)
{
    Console.WriteLine("a3大于等于10");
}
else if( a3 > 5 && a3 < 10 )
{
    Console.WriteLine("a3在6和9之间");
}
else if( a3 >= 0 && a3 <= 5 )
{
    Console.WriteLine("a3在0和5之间");
}
else
{
    Console.WriteLine("a小于0");
}
//对于初学者而言,代码逻辑要整齐,错落有致,方便对比嵌套逻辑语句块的配对

if语句的小练习--分辨奇偶数字

try
{
    console.writeline("请输入一个整数");
    int num = int.parse(console.readline());
    //能被2整除的数 叫偶数
    if (num % 2 == 0)
    {
        console.writeline("your input is even");
    }
    else
    {
        console.writeline("your input is odd");
    }
}
catch
{
    console.writeline("请输入数字");
}

语句块的知识
{}括起来的逻辑语句是一个代码块,注意变量在代码块中的生命周期

//语句块体悟
//语句块引起的变量的生命周期
//语句块中声明的变量只能在当前的语句块中使用
//体会当下代码在编译器中的报错意义!
int a = 1;
int b = 2;
{
    int b = 3;
    Console.WriteLine(a);
    Console.WriteLine(b);
}
Console.WriteLine(b);

int a = 5;
if (a > 3)
{
    int b = 0;
    ++b;
    b += a;
}
Console.WriteLine(b);
Switch 语句

当判断条件过多时候,使用if elseif 来进行判断时,需要写多条elseif,显得冗长繁琐,为此体现出switch分支语句的优势--清晰明了

//switch语句
int a=2;
switch(a)
{
   //这个条件一定是常量
    case 1:
        Console.WriteLine("a等于1");
        break;//每个条件之间通过break隔开 
    case 2:
        Console.WriteLine("a等于2");
        break;
    case 3:
        Console.WriteLine("a等于3");
        break;
    default://可省略 默认选择条件
        Console.WriteLine("什么条件都不满足,执行default中的内容");
        break;
}
string str = "123";
switch (str)
{
    case "123":
        Console.WriteLine("等于123");
        break;
    case "234":
        Console.WriteLine("等于234");
        break;
}
//贯穿使用
//当一个变量同时满足多个条件可以做多条件的“合并”判断
//给变量对号找家--如果找到相关的可以接受的便会直接匹配,
//否则会继续匹配下一条case条件
string name="畅知";
switch (name)
{
    //只要是符合三个条件之一就行
    case "畅知":
    case "TonyChang":
    case "小明":
    	Console.WriteLine("是个帅哥!");
    	break;//break有阻断作用
    case "小玉":
    case "莉莉":
    	Console.WriteLine("是个美女!");
    	break;
     default:
        break;
}

switch使用练习:学生成绩的分档

//输入学生的考试成绩,如果
//成绩 >= 90:A
//90 > 成绩 >= 80:B
//80 > 成绩 >= 70:C
//70 > 成绩 >= 60:D
//成绩 < 60:E
//(使用switch语法完成)
//最后输出学生的考试等级
try
{
    Console.WriteLine("请输入学生成绩");
    int cj = int.Parse(Console.ReadLine());
    // 取它的 十位数
    // 100 / 10 = 10
    // 99 / 10 = 9
    // 84 / 10 = 8
    // 74 / 10 = 7
    // cj = cj / 10;
    cj /= 10;
    switch (cj)
    {
        case 10:
        case 9:
            Console.WriteLine("你的成绩是A");
            break;
        case 8:
            Console.WriteLine("你的成绩是B");
            break;
        case 7:
            Console.WriteLine("你的成绩是C");
            break;
        case 6:
            Console.WriteLine("你的成绩是D");
            break;
        default:
            Console.WriteLine("你的成绩是E");
            break;
    }
}
catch
{
    Console.WriteLine("请输入数字");
}

循环语句

循环可以使满足循环执行条件的逻辑反复执行。注意不要随便写出死循环。

while循环
//while循环
int a=1;
while(a<10)//循环条件
{
    ++a;
}
Console.WriteLine(i);
//循环的嵌套使用
int a1=1;
int b=0;
while (a1 < 10)
{
    ++a1;
    while (b < 10)
    {
        ++b;
    }
}
//break的使用
//break可以是执行逻辑点跳出while语句块
 while (true)
{
    Console.WriteLine("break之前的代码");
    break;
    Console.WriteLine("break之后的代码");
}
Console.WriteLine("循环外的代码");
//continue的使用
//使执行逻辑点跳出当前的循环内容
//直接进入下一次的循环判断执行
//打印1到20之间的 奇数
int index = 0;
while(index < 20)
{
    ++index;
    //什么样的数是奇数
    //不能被2整除的数 ——> %
    if (index % 2 == 0)
    {
        continue;//跳过偶数情况
    }
    Console.WriteLine(index);
}

练习--找出100内所有素数打印

 //找出100内所有素数并打印。
int num = 2;
while( num < 100 )
{
    // 用想要判断是素数的数  从2开始 去取余 如果 中途就整除了 证明不是素数
    // 如果 累加到和自己一样的数了 证明是素数
    int i = 2;
    while( i < num )
    {
        //判断是否整除
        if( num % i == 0 )
        {
            break;
        }
        ++i;
    }
    if( i == num )
    {
        Console.WriteLine(num);
    }
    ++num;
}
doWhile循环

do……while语句与while循环差不多,只不过这个家伙太鲁莽,先斩后奏,不管如可,先执行代码块,再进行条件判断

//do while循环简单应用
string userName = "";
string passWord = "";
bool isShow = false;
do
{
    //这句代码 第一次 肯定不能执行
    if (isShow)
    {
        Console.WriteLine("用户名或密码错误,请重新输入");
    }
    //循环输入
    Console.WriteLine("请输入用户名");
    userName = Console.ReadLine();
    Console.WriteLine("请输入密码");
    passWord = Console.ReadLine();
    isShow = true;
} while (userName != "畅知" || passWord != "666");
for循环

for循环是最常使用的一种循环语句,

//for循环
for( int i = 10; i >= 0; i-- )
{
    Console.WriteLine(i);
}
//每个空位 可以按照规则进行书写 
//注意:分号不可以省去,即便没有变量声明也不可以省!
//第一个空位 声明变量 可以同时声明多个
//第二个空位 判断条件 返回值为bool
//第三个空位 对变量的操作 
for( int i = 0, j = 0; i < 10 && j < 0; ++i, j = j + 1)
{
}
//for循环的特殊使用
 // for循环可以写死循环
//for( ; ; )
//{
//    Console.WriteLine("for循环的死循环");
//}

int k = 0;
for(; k < 10; )
{
   ++k;//k++, k += 1;
}

//for( k = 0; ; ++k )
//{
//    if( k >= 10 )
//    {
//        break;
//    }
//}

for循环的经典练习:

一般都是找要执行逻辑块执行结果和循环条件变量之间的对应关系

//在控制台上输出如下10 * 10的空心星型方阵
//**********
//*        *
//*        *
//*        *
//*        *
//*        *
//*        *
//*        *
//*        *
//**********
//行
for (int j = 0; j < 10; j++)
{
    //列
    for (int i = 0; i < 10; i++)
    {
        //列 如果是 第1行和最后1行 那么 内层列循环 都打印星号
        // 按照 **********的规则打印
        if (j == 0 || j == 9)
        {
            Console.Write("*");
        }
        //否则 就是 按照*         *的规则打印
        else
        {
            if (i == 0 || i == 9)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
    }
    Console.WriteLine();
}
//在控制台上输出如下10行的三角形方阵
//         *            1    1   -> 2i - 1    9    10 - i
//        ***           2    3   -> 2i - 1    8    10 - i
//       *****          3    5                7    10 - i
//      *******         4    7                6    10 - i
//     *********        5    9                5
//    ***********       6    11               4
//   *************      7    13               3
//  ***************     8    15               2
// *****************    9    17               1
//*******************   10   19               0    10 - i
//行
for (int i = 1; i <= 10; i++)
{
    //打印空格的列
    for (int k = 1; k <= 10 - i; k++)
    {
        Console.Write(" ");
    }

    //打印星号的列
    for (int j = 1; j <= 2*i-1; j++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

//在控制台上输出九九乘法表
for (int i = 1; i <= 9; i++)
{
   //1 1 X 1 = 1 空行
   //2 1 X 2 = 2 2 X 2 = 4 空行
   //3 1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 空行
   for (int j = 1; j <= i; j++)
    {
        Console.Write("{0}X{1}={2}   ", j, i, i * j);
    }
    Console.WriteLine();
}
 //求1~100之间所有偶数的和
int sum = 0;
for (int i = 1; i <= 100; i++)
{
    //判断是否是偶数 是否能整除2
    if( i % 2 == 0 )
    {
        sum += i;
    }
}
for (int i = 2; i <= 100; i += 2)
{
    sum += i;
}
Console.WriteLine(sum);

标签:语句,10,Console,C#,break,--,int,WriteLine,case
From: https://www.cnblogs.com/TonyCode/p/17755122.html

相关文章

  • CentOS 7挂载命令及使用详解
    本文目录导读:前言什么是挂载命令CentOS7挂载命令的语法CentOS7挂载命令的使用方法挂载USB设备挂载网络共享文件夹CentOS7挂载命令的常见选项CentOS7挂载命令的注意事项前言在CentOS7操作系统中,挂载(mount)是一个常见的操作,用于将外部设备或存储空间与文件系统进行......
  • 【分享】影刀使用xpath捕获指定的元素
    xpath捕获元素比较精准,前面也介绍了xpath的用法现在捕获社区里帖子详情页的标题//*[@class='discuss_detail_header___3LhnQ']/h1找到class是discuss_detail_header___3LhnQ的子元素h1获取文章内容//*[@id='w-e-textarea-1']找到id是w-e-textarea-1的元素获取元素......
  • WARNING: too many parse errors
    1.19.16RAC库,alter日志告警2023-10-10T16:09:24.562724+08:00WARNING:toomanyparseerrors,count=32819SQLhash=0x3876c7a0PARSEERROR:ospid=239197,error=942forstatement:Additionalinformation:hd=0x363f03440phd=0x363f03878flg=0x100476cisid=114sid=11......
  • [CF1672G]Cross Xor
    G-CrossXor对于\((n\&1)\&\&(m\&1)\)的情况,所有行、列的异或和的必须相等(异或和指当前行/列中所有元素的异或和)每次修改的点\((x_1,y_1)\),\((x_2,y_1)\),\((x_1,y_2)\),\((x_2,y_2)\)使得所有行和列的异或和不会改变只对\((i,j)\)进行一次操作,那么所有行和列的异或和都会......
  • 总结selenium 中 js 更改隐藏属性
    第一种多个元素被隐藏时通过js修改对比照片这个是没隐藏的 对比照片 这个是隐藏的 driver=webdriver.Chrome()#urlurl=r"http://127.0.0.1:5000/"driver.get(url)print("已打开网页")#执行js脚本,将元素的display属性设置为block,......
  • 【Linux】Alpine 固定ip
    vi/etc/network/interfacesautoeth0ifaceeth0inetstaticaddress192.168.31.4netmask255.255.255.0gateway192.168.31.2dns-nameservers192.168.31.2hostname$(hostname)vi/etc/resolv.confnameserver192.168.31.2重启 /etc/init.d/networkingresta......
  • YARN
    YARN基础 概述可以把HadoopYARN理解为相当于一个分布式的操作系统平台,而MapReduce等计算程序则相当于运行于操作系统之上的应用程序,YARN为这些程序提供运算所需的资源(内存、CPU等)。ApacheHadoopYARN(YetAnotherResourceNegotiator,另一种资源协调者)是一种新的Hadoop......
  • [arc135f] Delete 1, 4, 7, ...
    F-Delete1,4,7,...设\(f(i)\)表示第一次操作后,第\(i\)个位置的数,那么\(f(i)=\lfloor\frac{3i+1}2\rfloor\)那么\(k\)次操作后,第\(i\)个位置上的数就是:\[f(f(...f(f(i))...))=f^k(i)\]设\(cnt_k\)表示\(k\)次操作后剩下的数的个数,那么显然有:\(cnt_i=\lfloor\frac{cnt_......
  • 记一个Elmessage被遮挡问题
    之前在开发一个管理页面,功能有,编辑时只有一行可以编辑,删除时弹出警告窗口,确认后才执行删除。​代码为Element-plus中的示例。但是ElMessageBox一直被遮挡代码如下,均为Element-plus的示例,此外还有两层router-view嵌套:<template><el-table:data="projectTableData"sty......
  • 建造者模式
    一、概念建造者模式使用简单的对象一步一步构建一个复杂的对象。应用场景:在软件系统中,有时需要创建一个复杂对象,其通常由各个部分的子对象用一定的算法构成。由于需求的变化,这个复杂对象的各个部分会有所不同,但是它们组合在一起的算法是相对稳定的。二、实现我们假设......