zoukankan      html  css  js  c++  java
  • 语句--分支语句if case

         语句是指程序命令,都是按照顺序执行的。语句在程序中的执行顺序称为“控制流”或者“执行流”。根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同。

    语句可以嵌套,可以是以分号结尾的单行代码,也可以是语句块中的单行语句。语句块括在括号{}中,并且可以包含嵌套。

        语句的类型包括声明语句,表达式语句,选择语句,循环语句,跳转语句,异常语句。

    1.声明语句

    引入新的变量或者常量。变量声明可以选择为变量赋值。在常量声明中必须赋值。

    2.表达式语句

    用于计算值的表达式语句必须在变量中储存该值

    3.选择语句

    if,else,switch,case

    4.循环语句

    do,for,forerch,while

    5.跳转语句

    break,continue,default,return

    6.异常语句

    try-catch-finally

    一。选择语句

    if,else

    if是如果的意思,else是另外的意思,if后面跟(),括号内为判断条件,如果符合条件则进入if语句执行命令。如果不符合则不进入if语句。else后不用加条件,但是必须与if配合使用,else后也可加if,但if后需要条件。if-else可以嵌套。

    其格式有以下几种:

    格式1:if(...)//括号内是判断条件

                {

                 //程序代码,运算等等

                 }

     格式2:if()

                {

                 }

                 else

                 {

                  }

    格式3:if()

              {

               }

               else if()

               {

                }

    格式4:if()

               {

                }

               if()

                {

                 }

                else

                {

                 }

     从语句开始主要依靠练习来熟练掌握。以下是做过的一些练习

    输入三个整数,xyz,最终以从小到大的方式输出,利用嵌套
    Console.Write("请输入一个整数x=");
    int x = int.Parse(Console.ReadLine());
    Console.Write("请输入一个整数y=");
    int y = int.Parse(Console.ReadLine());
    Console.Write("请输入一个整数z=");
    int z = int.Parse(Console.ReadLine());

    if (x > y)
    {
    if (x > z && y > z)
    {
    int a = x;
    int b = y;
    int c = z;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    else if (x > z && z > y)
    {
    int a = x;
    int b = z;
    int c = y;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    else if (z > x)
    {
    int a = z;
    int b = x;
    int c = y;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    }
    else
    {
    if (y > z && x > z)
    {
    int a = y;
    int b = x;
    int c = z;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    else if (y > z && z > x)
    {
    int a = y;
    int b = z;
    int c = x;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    else if (z > x)
    {
    int a = z;
    int b = y;
    int c = x;
    Console.WriteLine("三个数的大小依次是" + c + " " + b + " " + a);
    }
    }

    Console.ReadLine();


    输入学生姓名,输入考试成绩 double
    若是100,【恭喜你***,满分通过!】
    若是大于等于80小于100,【**,你很优秀,继续保持!】
    若是大于等于60小于80,【**成绩良好】
    大于等于50小于60,【**就差一点点,下次一定要至少及格!】
    小于50,【**你是笨蛋么?】

    Console.Write("请输入学生姓名");
    string name = Console.ReadLine();
    Console.Write("请输入考试成绩");
    double a = double.Parse(Console.ReadLine());
    if (a < 0|| a > 100)
    {
    Console.WriteLine("输入错误");
    }
    else if (a == 100)
    {
    Console.WriteLine("恭喜你{0},满分通过", name);
    }
    else
    {
    if (a > 80)
    {
    Console.WriteLine("{0}你很优秀,继续保持!", name);
    }
    else if (a <= 80 && a >= 60)
    {
    Console.WriteLine("{0}成绩良好", name);
    }
    else if (a <= 60 && a >= 50)
    {
    Console.WriteLine("{0}就差一点点,下次一定要至少及格!", name);
    }
    else
    {
    Console.WriteLine("你是笨蛋么?", name);
    }
    }
    Console.ReadLine();

    有一组函数:y = x (x<1);y = 2x -1 (1<=x<10); y = 3x-11 (x>=10)。
    括号内是x的满足条件。
    实现功能,随意输入一个x值,输出y的值。

    Console.Write("请输入一个x值");
    double x = double.Parse(Console.ReadLine());
    double y;
    if (x < 1)
    {
    y = x;
    Console.WriteLine("y=" + y.ToString());
    }
    else if (x >= 1 && x < 10)
    {
    y = x * 2 + 1;
    Console.WriteLine("y=" + y.ToString());
    }
    else
    {
    y = x * 3 - 11;
    Console.WriteLine("y=" + y.ToString());
    }
    Console.ReadLine();

    输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上数字,否则输出两数之和
    Console.Write("请输入一个数a=");
    int a = int.Parse(Console.ReadLine());
    Console.Write("请输入一个数b=");
    int b = int.Parse(Console.ReadLine());
    if (a * a + b * b > 100)
    {
    int c = a * a + b * b;
    Console.WriteLine(c / 100);
    }
    else
    {
    Console.WriteLine(a + b);
    }
    Console.ReadLine();


    相亲过程:你有房子么?你有钱么?你有能力么?
    【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】
    利用if嵌套做相亲过程
    Console.Write("你有房子么?(请回答有或者没有) ");
    string a = Console.ReadLine();
    if (a == "有")
    {
    Console.Write("结婚吧");
    }
    else if (a == "没有")
    {
    Console.Write("你有钱么?(请回答有或者没有) ");
    string b = Console.ReadLine();
    if (b == "有")
    {
    Console.Write("先买房子在结婚");
    }
    else if (b == "没有")
    {
    Console.Write("你有能力么?(请回答有或者没有) ");
    string c = Console.ReadLine();
    if (c == "有")
    {
    Console.Write("先赚钱再买房子再结婚");
    }
    else if (c == "没有")
    {
    Console.Write("拜拜~~");
    }
    }
    }
    Console.ReadLine();


    Console.WriteLine("求方程式 ax*x+bx+c=0");
    Console.Write("请输入a=");
    double a = double.Parse(Console.ReadLine());
    Console.Write("请输入b=");
    double b = double.Parse(Console.ReadLine());
    Console.Write("请输入c=");
    double c = double.Parse(Console.ReadLine());
    double de = b * b - 4 * a * c;
    if (a == 0)
    {
    Console.WriteLine("这不是一元二次方程");
    }
    else
    {
    Console.WriteLine("这是一元二次方程");
    if (de >= 0)
    {
    double x1=(-b+Math.Sqrt(de))/(2*a);
    double x2 = (-b - Math.Sqrt(de)) / (2 * a);
    if (de > 0)
    {
    Console.WriteLine("方程式有两个不同的实根");
    Console.WriteLine("x1=" + x1.ToString() + " x2=" + x2.ToString());
    //Console.WriteLine("x1=" + x1 + " x2=" + x2);
    }
    else
    {
    Console.WriteLine("方程式有两个相同的实根");
    Console.WriteLine("x1=x2=" + x1.ToString());
    //Console.WriteLine("x1=x2=" + x1);
    }
    }
    else if (de < 0)
    {
    Console.Write("方程式没有实根");
    }
    }
    Console.ReadLine();


    输入一个年份,判断是否是闰年
    能被4整除却不能被100整除的年份。a%4==0&&a%100!=0
    世纪年份能被400整除的是闰年。a%100==0

    输入年月日,判断时间日期格式是否正确
    年:0~9999
    月:1~12
    日:1 3 5 7 8 10 12 31天
    4 6 9 11 30天
    2闰年29天,平年28天

    Console.Write("请输入您要查询的年份 ");
    int year = int.Parse(Console.ReadLine());


    if (year < 0 || year > 9999)
    {
    Console.WriteLine("您输入的年份有误");
    }
    else
    {
    Console.Write("您输入的年份是{0},请输入要查询的月份 ", year);
    int moon = int.Parse(Console.ReadLine());
    if (moon < 0 || moon > 12)
    {
    Console.WriteLine("您输入的月份有误");
    }
    else
    {
    Console.Write("您输入的月份是{0},请输入要查询的日期 ", moon);
    int day = int.Parse(Console.ReadLine());
    if (day < 0 || day > 31)
    {
    Console.WriteLine("您输入的日期有误");
    }
    else
    {
    if (year % 4 == 0 && year % 100 != 0 && year % 100 == 0)
    {
    if (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    else if (moon == 4 || moon == 6 || moon == 9 || moon == 11)
    {
    if (day > 30)
    {
    Console.WriteLine("您输入的日期有误");
    }
    else
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    }
    else
    {
    if (day > 30)
    {
    Console.WriteLine("您输入的日期有误");
    }
    else
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    }
    }
    else
    {
    if (moon == 1 || moon == 3 || moon == 5 || moon == 7 || moon == 8 || moon == 10 || moon == 12)
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    else if (moon == 4 || moon == 6 || moon == 9 || moon == 11)
    {
    if (day > 30)
    {
    Console.WriteLine("您输入的日期有误");
    }
    else
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    }
    else
    {
    if (day > 29)
    {
    Console.WriteLine("您输入的日期有误");
    }
    else
    {
    Console.Write("您输入的日期是{0},您要查询的日期{1}--{2}--{3} ", day, year, moon, day);
    }
    }
    }
    }

    }
    }

    Console.ReadLine();

    愿我有生之年,得见您君临天下。 吾辈必当勤勉,持书仗剑耀中华。
  • 相关阅读:
    git push 失败出现error: src refspec master does not match any.解决方案
    git:not a git repository (or any of the parent directories)
    初次安装git配置用户名和邮箱
    MYSQL的C API之mysql_query
    PyCharm + PyQt4 环境搭建
    pycharm下打开、执行并调试scrapy爬虫程序
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/bloodPhoenix/p/5597781.html
Copyright © 2011-2022 走看看