与你相识之运算符、逻辑运算符、循环

书接上篇之回顾C#运算符、逻辑运算符、循环

一. C# 常量、变量
1)变量:变量是不固定的值通过在等号后跟一个常量表达式初始化
int a = 0, b = 1; (赋值)
2)常量:常量是不可变的值使用 const 定义
const a=0;
二. C#运算符
1) + 、 - 、* 、/ 、%
2)++ 、–
a++ == (a=a+1) b-- ==(b=b-1)

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

namespace FirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            #region  初识c#
            //Console.Write("Hello World");
            //int a = 1;
            //float b = 1.00f;
            #endregion

            int a = 1;
            int b = 10;
            #region 运算符
            // 单行注释
            /** 多行注释 **/
            // {0} 占位符  \n 换行

            Console.Write(string.Format("{0}\n", a + b));
            //Console.Write(b - a);
            //a++;
            //b--;
            //Console.Write(a);
           // Console.Write(b);
            //Console.Write(b * a);
           // Console.Write(b / a);
           // Console.Write(b % a);
            #endregion
            Console.ReadKey();
        }
    }
    }

三 .C#关系运算符、逻辑运算符
1) += 、 -= 、==、> 、<、>= 、<=、!=

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

namespace FirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
  
            #region 关系运算符
            //bool c = b > a;
            //Console.Write(c);
            // a+=1 ;
            // b -= 1;
            //Console.Write(string.Format("{0}\n", a));
            //Console.Write(b);

            //bool c = b >= a;
            //bool d = a<=b;
            //Console.Write(string.Format("{0}\n", c));
            //Console.Write(d);
            bool c = b != a;
            Console.Write(c);
            #endregion
            Console.ReadKey();
        }
    }
    }

C# if 、if-else、if-else if-else、switch

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

namespace FirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            #region if
            //int a = 1;
            //int b = 10;
            //if (b > a) {
            // Console.Write("b确实比a大");
            //}
            #endregion
            #region if else
            //int a = 1;
            //int b = 10;
            //if (a > b)
            //{
            //    Console.Write("a确实比b大");
            //}
            //else {
            //    Console.Write("a比b小");
            //}
            #endregion
            #region if的三种情况
            //int a = 10;
            //int b = 10;
            //if (a > b)
            //{
            //    Console.Write("a确实比b大");
            //}
            //else if (b > a)
            //{
            //    Console.Write("a比b小");
            //}
            //else {
            //    Console.Write("都不满足");
            //}
            #endregion
            // if 满足条件只执行一种情况
            // if else  满足条件只执行if 否则执行else
            // if else-if else 执行满足条件的if或else-if 否则执行else
            #region switch

            Console.WriteLine("请输入a的值");
            string a=Console.ReadLine();

            switch (a)
            {
               case "1":
                    Console.Write("满足条件a=1");
                   break;
                case "2":
                    Console.Write("满足条件a=2");
                    break;
                case "3":
                    Console.Write("满足条件a=3");
                    break;
                default:
                   Console.Write("都满足");
                    break;
            }
            #endregion
             Console.ReadKey();
            //if else 与switch 比较
            //if else 可以处理范围  switch多用于比较值
         }
     }
 }

四 .C# 循环

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

namespace FirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        
            #region 循环
            //int a = 1;
            //while (a<10)//满足条件跳出循环
            //{
            //    a++;
            //    Console.Write(a);
            //}
            //string a = "";
            //do
            //{
            //    Console.WriteLine("请输入5结束循环");
            //    a = Console.ReadLine();
            //} while (a != "5");
            //Console.Write("满足条件结束循环");

            //while do-while 区别 while满足条件进入循环体。 do-while 先执行一次循环体,满足条件再进循环体  ===>逻辑等于True
            #region for
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(i);
            //}
            #endregion
            #region foreach
            string[] list = { "1", "2", "3", "4", "5" };
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }
            #endregion
              #region for 嵌套循环之冒泡排序
            int[] num = { 8, 6, 3, 2, 1, 5, 4, 7, 9 };
            //外层循环比较 6 3 2 1 5 4 7 8 9 循环length-1次
            //内层循环交换位置 3 2 1 5 4 6 7 8  9 交换length-1次
            //当交换位置完成时最后一位为最大数不用再交换,最终交换次数 length-1-i
            // 当比较时比后一位小不用交换
            for (int i = 0; i < num.Length-1; i++)
            {
                for (int j = 0; j < num.Length-1-i; j++)
                {
                    if (num[j]>num[j+1])
                    {
                        int tem = num[j];
                        num[j] = num[j+1];
                        num[j+1] = tem;
                    }
                   
                }
                
            }
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine(num[i]);
            }
           
            #endregion
            #endregion
             Console.ReadKey();
        }
     }
 }

今天先到这里,改天继续。这里运算符、占位符、转义符、注释、代码块、逻辑运算符、循环 这些东西初次接触的多练练很容易懂。【这里没一一校验代码有问题没,我是从一个方法内复制出来分开的】那个地方解释不到位的,度娘要用好。只要思想不滑坡,方法总比困难多~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值