zoukankan      html  css  js  c++  java
  • 知识点-语句

    数据类型-变量常量-运算符(表达式)-语句(顺序、分支、循环)-数组-函数

    补充:%(取余)的用处
            判断一个数能否被整除,如果能被整除,余数是0;分类

    案例        

    static void Main111(string[] args)
    {
    //从键盘输入一个两位数字,判断是否与7有关
    int a;
    bool ok1=true,ok2=true,ok3=true,ok;
    //输入
    a = Convert.ToInt32(Console.ReadLine());
    //运算
    //1.被7整除
    ok1 = a % 7 == 0;
    //2.个位数是7
    ok2 = a % 10 == 7;
    //3.十位数是7
    ok3 = a / 10 == 7;

    ok = ok1 || ok2 || ok3;

    //输出
    string jieGuo = ((ok == true) ? "与7相关" : "与7无关");
    Console.WriteLine(jieGuo);
    }

    分支语句

    1.if

    if(表达式)
    {
    语句
    }

    注意:
    1.如果,表达式成立,只执行一条语句的话,可以省去花括号。如果表达式成立,需要执行的语句数量大于等于2条,必须把这些句语放在花括号中。
    2.if的小括号后,不要加分号。

    2.if...else...
    if(表达式)
    {
    }
    else
    {
    }

    注意:同if
    1.可以有if没有else,但是有else,前面必须有if.
    2.else后面没有括号和条件表达式。
    3.满足条件走if后的花括号,直接就忽略else

    案例1    

    //输入三个数,三个数中找出最大的来

    static void Main(string[] args)
    {
    int a, b, c;
    int zuida;
    a = Convert.ToInt32( Console.ReadLine());
    b = Convert.ToInt32(Console.ReadLine());
    c = Convert.ToInt32(Console.ReadLine());

    if (a > b)
    {
    zuida = a;
    }
    else
    {
    zuida = b;
    }


    if (zuida < c)
    {
    zuida = c;
    }

    Console.WriteLine(zuida);
    }

    案例2

    //判断输入的年份是闰年,还是平年

    static void Main333(string[] args)
    {
    Console.Write("请输入一个年份:");
    int year = Convert.ToInt32(Console.ReadLine());

    //1.被400整除; 2.被4整除,但不能被100整除
    if(year%400==0 || (year%4==0 && year%100!=0))
    {
    Console.WriteLine("闰年");
    }
    else
    {
    Console.WriteLine("平年");
    }

    3.多分支
    if... else if .... else if.....else
    if(表达式1)
    {
    语句1;
    }
    else if(表达式2)
    {
    语句2;
    }
    ...
    else
    {
    语句n;
    }

    案例1:

    static void Main(string[] args)
    {
    Console.Write("请输入年份:");
    int year = Convert.ToInt32(Console.ReadLine());
    Console.Write("请输入月份:");
    int month = Convert.ToInt32(Console.ReadLine());

    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
    Console.WriteLine("本月是31天");
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
    Console.WriteLine("本月是30天");
    }
    else if (month == 2)
    {
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
    {

    Console.WriteLine("29天");

    }
    else
    {
    Console.WriteLine("28天");
    }
    }
    else
    {
    Console.WriteLine("月份有问题");
    }
    }

    案例2

    static void Main(string[] args)
    {
    Console.WriteLine("请输入年龄:");
    int age = Convert.ToInt32(Console.ReadLine());

    if (age > 0 && age < 6)
    {
    Console.WriteLine("童年");
    }
    else if (age >= 6 && age < 16)
    {
    Console.WriteLine("少年");
    }
    else if (age >= 16 && age < 30)
    {
    Console.WriteLine("青年");
    }
    else if (age >= 30 && age < 60)
    {
    Console.WriteLine("中年");
    }
    else if (age > 60 && age < 130)
    {
    Console.WriteLine("老年");
    }
    else
    {
    Console.WriteLine("输入有误");
    }
    }

    4.if嵌套

    if(表达式)

    {
    if(表达式)
    {
    }
    }
    else
    {
    if(表达式)
    {
    }
    else
    {
    }
    }

    案例1

    做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布
    要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。

    计算机生成随机数:
    Random rand = new Random();
    int c = rand.Next(3);

    static void Main(string[] args)
    {
    //计算机出拳
    Random rand = new Random();
    int c = rand.Next(3);
    //人出拳
    Console.Write("请出拳");
    int r = Convert.ToInt32(Console.ReadLine());

    if (c == 0)
    {
    if (r == 0)
    {
    Console.WriteLine("计算机与人打平");
    }
    else if (r == 1)
    {
    Console.WriteLine("人赢计算机");
    }
    else if (r == 2)
    {
    Console.WriteLine("计算机赢人");
    }
    else
    {
    Console.WriteLine("出拳错误");
    }
    }
    else if (c == 1)
    {
    if (r == 0)
    {
    Console.WriteLine("计算机赢人");
    }
    else if (r == 1)
    {
    Console.WriteLine("计算机与人打平");
    }
    else if (r == 2)
    {
    Console.WriteLine("人赢计算机");
    }
    else
    {
    Console.WriteLine("出拳错误");
    }
    }
    else if (c == 2)
    {
    if (r == 0)
    {
    Console.WriteLine("人赢计算机");
    }
    else if (r == 1)
    {
    Console.WriteLine("计算机赢人");
    }
    else if (r == 2)
    {
    Console.WriteLine("计算机与人打平");
    }
    else
    {
    Console.WriteLine("出拳错误");
    }
    }
    }

    案例2

    做一个算缘分的小游戏:
    输入男方姓名,女方姓名,输出缘分指数,给出建议。

    static void Main(string[] args)
    {
    Console.Write("男方姓名:");
    string nan = Console.ReadLine();
    Console.Write("女方姓名:");
    string nv = Console.ReadLine();

    Random rand = new Random();
    int n = rand.Next(100);
    n++;

    string jianYi = "";
    if (n > 0 && n < 30)
    {
    jianYi = "分手吧";
    }
    else if (n >= 30 && n < 60)
    {
    jianYi = "一起努力";
    }
    else if (n >= 60 && n <= 80)
    {
    jianYi = "幸福一对";
    }
    else
    {
    jianYi = "鸳鸯配";
    }

    Console.WriteLine("{0}和{1}的缘分指数是:{2}。建议:{3}", nan, nv, n,jianYi);
    }

    案例3

    axx+bx+c==0(a!=0)。输入a,b,c给这个一元二次方程,显示根的个数?

    static void Main(string[] asdfasdf)
    {
    Console.WriteLine("一元二次方程a*x*x+b*x+c == 0,请输入a,b,c的值");
    int a = Convert.ToInt32(Console.ReadLine());
    int b = Convert.ToInt32(Console.ReadLine());
    int c = Convert.ToInt32(Console.ReadLine());

    if (a == 0)
    {
    Console.WriteLine("不是一元二次方程");
    }
    else
    {
    int delta = b * b - 4 * a * c;
    if (delta > 0)
    {
    Console.WriteLine("两个不等实根");
    }
    else if (delta == 0)
    {
    Console.WriteLine("两个相等实根");
    }
    else
    {
    Console.WriteLine("无实根");
    }
    }
    }

    No Excuse~
  • 相关阅读:
    Docker-Compose搭建单体SkyWalking 6.2
    Docker搭建MySQL主从集群,基于GTID
    【简记】修改Docker数据目录位置,包含镜像位置
    【拆分版】Docker-compose构建Kibana单实例,基于7.1.0
    【拆分版】Docker-compose构建Logstash多实例,基于7.1.0
    【拆分版】Docker-compose构建Zookeeper集群管理Kafka集群
    命令行模式和python交互模式
    详解 HTTPS、TLS、SSL、HTTP区别和关系
    windows下如何查看进程、端口占用、杀死进程教程
    pycharm最常用的快捷键总结
  • 原文地址:https://www.cnblogs.com/jinshui/p/5389117.html
Copyright © 2011-2022 走看看