zoukankan      html  css  js  c++  java
  • 总结

    1、基本语法:
    数据类型,类型转换,输入输出,运算符(条件运算符) ? :

    2、分支语句:
    有选择性的执行某行或某段代码
    if ... else if ... else... --最主要,必须要记住并且灵活运用
    switch ... case ... default... --看到要明白什么意思

    分支嵌套:在复合一定条件的基础上继续判断符合其它条件

    3、循环语句:
    循环四要素:初始条件,循环条件,循环体,状态改变
    for() //最常用,最好用,最清晰,最灵活
    {

    }
    必须记住,掌握,灵活运用

    while() //不够清晰
    {

    }

    foreach() //遍历,数组,集合(泛型集合)
    {

    }

    循环的嵌套:
    一般只需要套两层循环就足够了,外面的循环走一遍,里面的循环会走一整遍

    注意:中间变量的运用,要灵活掌握

    练习1

    用户输入一个字母,如D,那么就打印 ABCDCBA

                //用户输入一个字母,如D,那么就打印 ABCDCBA
                Console.Write("请输入一个字母");
                char a = Convert.ToChar(Console.ReadLine());
                char b = 'a';
                bool c = true;//判断b++或是b--
                //拼接要打印的内容
                for (int i = 1; i <= 26; i++)
                {
                    Console.Write(b);
                    if (b == a)
                    {
                       c= false;
                    }
                    if (c)
                    {
                        b++;
                    }
                    else
                    {
                        b--;
                        if (b == 'a' || b == 'A')
                        {
                            Console.Write(b);
                            break;
                        }
                    }
    
                }
               
                    Console.ReadLine();

    运算结果

    练习2

    一张纸,对折几次可以达到珠穆朗玛峰的高度?
    一张纸为3微米,10微米为1毫米,珠峰高度为8848米

                //一毫米等于1000微米,一米等于1000毫米
                long a = 3;//纸的厚度
                long feng = 8848000000;//峰的高度
                long count = 1;//次数
                for (; ; )
                {
                    a *= 2;
                    count++;
                    if (a >= feng)
                    {
                        Console.WriteLine("纸的高度是"+a+"微米。");
                        Console.WriteLine("珠峰的高度"+feng+"微米。");
                        Console.WriteLine("需要折"+count+"次。");
                        break;
                    }
                }        
    
                    Console.ReadLine();

    运算结果

    练习3

    让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成

                Console.Write("请输入一个数字:");
                try
                {
                    int a = Convert.ToInt32(Console.ReadLine());
                    if (a % 2 != 0)
                    {
                        for (int i = 1; i <= (a+1)/2; i++)//菱形上半部分
                        {
                            int c = ((i * 2 - 1) + 1) / 2 - 1;//中间值
                            string end = "";
                            char  b='a';
                            int count = 0;
                            bool d = false;
                            for (int j = 1; j <=(a+1)/2-i; j++)
                            {
                                end += " ";
                            }
                            for (int j = 1; j <= i * 2 - 1; j++)
                            {
                                end += b;
                                if (count == c)
                                {
                                    d = true;
                                }
                                if (d)
                                {
                                    if (b == 'A')
                                    {
                                        b = 'Z';
                                    }
                                    else
                                    {
                                        b--;
                                    }
                                }
                                else
                                {
                                    if (b == 'Z')
                                    {
                                        b = 'A';
                                    }
                                    else
                                    {
                                        b++;
                                    }
                                    count++;
                                }
                            }
                            Console.WriteLine(end);
                        }
                        for (int i = 1; i < (a + 1) / 2; i++)//菱形的下半部分
                        {
                            char b = 'a';
                            int c = ((a - i * 2)+1) / 2 - 1;
                            string end = "";
                            int count = 0;
                            bool d = false;
                            for (int j = 1; j <= i; j++)
                            {
                                end += " ";
                            }
                            for (int j = 1; j < (a - i * 2) + 1; j++)
                            {
                                end += b;
                                if (count == c)
                                {
                                    d = true;
                                }
                                if (d)
                                {
                                    if (b == 'A')
                                    {
                                        b = 'Z';
                                    }
                                    else
                                    {
                                        b--;
                                    }
                                }
                                else
                                {
                                    if (b == 'Z')
                                    {
                                        b = 'A';
                                    }
                                    else
                                    {
                                        b++;
                                    }
                                    count++;
    
                                }
                            }
                            Console.WriteLine(end);
                        }
                    }
                    else
                    {
                        Console.Write("输入错误");
                    }
                }
                catch
                {
                    Console.Write("输入错误");
                }
    
                Console.ReadLine();

    运算结果

  • 相关阅读:
    例20:希尔排序
    例19:直接插入排序
    例14:计算某日是该年的第几天
    为自己
    hdoj--1027--Ignatius and the Princess II(dfs)
    UESTC--758--P酱的冒险旅途(模拟)
    nyoj--990--蚂蚁感冒(模拟)(思维题)
    历届试题 邮局(dfs+剪枝)
    历届试题 数字游戏
    历届试题 回文数字
  • 原文地址:https://www.cnblogs.com/sunshuping/p/5526831.html
Copyright © 2011-2022 走看看