zoukankan      html  css  js  c++  java
  • 2011.11.28 C#笔记

    char s =(char) Console.Read(); 
    //Read返回值是int,ReadLine返回值是string,所以不能显式转换
    // char s =(char) Console.ReadLine(); 错误
    if (char.IsUpper(s))//判断是否是大写
    {
    Console.WriteLine(s);

    }

    else if (char.IsLower(s))//判断是否是小写
    {

    }
    else if (char.IsDigit(s))//判断是否是数字
    {

    }


    int i=3;
    switch(i=3)
    {
    case 1:
    i++;
    break;
    case 2:
    i--;
    break;
    case 3:
    i++; //不能写i+3,i+1这类的因为s=i+3;s=i+1;这样写。
    break;

    }

    Console.WriteLine(i);


    string s = Console.ReadLine().Trim(); //移除前导和尾部的所有空格

    char.IsUpper() 判断是否是大写
    char.IsDigit()判断是否是数字
    char.IsLower() 判断是否是小写

    char.ToUpper() 转换成大写
    char.ToLower() 转换成小写


    //string s = "今天你好吗?";
    //string s1 = s.Substring(2,4); //从索引处 如果长度 大于剩余的字符串长度会出现超异常
    //Console.WriteLine(s1);


    StringBuilder sb = new StringBuilder("出赛",100); //定义一个新的对象,new后面是 一个对象,sb是一个变量而已,这是引用类型的
    sb.Insert(2,"(作者:吴安)\n");//在索引第2开始插入内容 0 1 2
    sb.Append("万里长征人未还,\n秦时明月汉时关。\n但使龙城飞将在,\n不教胡马度阴山。"); //在尾部添加,注意这里的\n
    sb.Remove(11, 2); //移除 索引从第11开始的 2个字符
    sb.AppendFormat("\n\n{0:D}",DateTime.Now.ToString()); //添加当前时间
    Console.WriteLine(sb);




    //输出一串字符串然后颠倒显示
    Console.WriteLine("请输入一串字符串:");//提示
    string str = Console.ReadLine();//接受一串字符串
    char[] charToStr = str.ToCharArray();//把字符串转换成char一元数组
    Array.Reverse(charToStr);//把字符串颠倒
    Console.WriteLine(charToStr);


    //去除字符串中的所有空格:
    Console.WriteLine("请输入一串字符串:");
    string words = Console.ReadLine().Trim();//先去掉首尾的空格

    string temp = words.Replace(" ","");//更简单的方法

    Console.WriteLine(temp);


    //StringBuilder newWords=new StringBuilder(words);
    //StringBuilder secondWords=new StringBuilder();//定义一个新的数组
    //for (int i = 0; i < newWords.Length; i++)
    //{

    // if(char.IsWhiteSpace(newWords[i]))//判断第i位是否是空格
    // {
    // continue;//是空格就结束本次循环
    // }

    // secondWords.Append(newWords[i]);

    //}

    //Console.WriteLine(secondWords);


    ----------数组
    string[] strs1 = new string[3] { "1", "2", "3" };
    string[] strs2 = {"1","2","3"}; //同上

    //自定义一个动态2维数组,并输出2维数组
    int i,j;
    Console.WriteLine("输入行数:");
    i = Convert.ToInt32(Console.ReadLine()); //确定行数
    Console.WriteLine("输入列数:");
    j = Convert.ToInt32(Console.ReadLine());//确定列数
    int[,] data = new int[i, j];//定义并初始化2维数组
    for (int row = 0; row < i; row++)//从0开始循环
    {
    for (int col = 0; col < j; col++)//循环列数
    {
    Console.Write("A[{0}][{1}]:", row,col);
    data[row,col] = Convert.ToInt32(Console.ReadLine());
    }
    }
    for (int row = 0; row < i; row++)
    {

    for (int col = 0; col < j; col++)
    {
    Console.Write("{0}\t", data[row, col]);// \t是为了 方便阅读
    }
    Console.WriteLine();
    }
    Console.ReadKey();



    //输出一个2维数组:
           int[,] ints = { { 1, 2 }, { 3, 4 } };//定义并初始化一个2维数组

    //Console.WriteLine(ints.Rank);//输出行数
    //Console.WriteLine(ints.GetUpperBound(ints.Rank - 1) + 1); //输出列数

    //Console.WriteLine(ints[0,0]);

    for (int i1 = 0; i1 < ints.Rank; i1++)
    {
    for (int i2 = 0; i2 < ints.GetUpperBound(ints.Rank - 1) + 1; i2++)
    {
    Console.WriteLine("ints[{0},{1}]是{2}", i1, i2, ints[i1, i2]);
    }
    }

    //Console.WriteLine(ints[0,0]);

    Console.ReadLine();


    //排序从小到大-------冒泡排序法
    int[] ints = { 1,9,3,17,16,13}; //定义一个一维数组 0-5
    foreach (int n in ints)//遍历显示数组
    {
    Console.Write(n+" ");
    }

    int j, temp;
    for (int i = 0; i < ints.Length-1; i++) //这里为什么-1,因为当最后一个数字是5的时候,j=6是不存在的,所以要-1
    {
    j = i + 1;
    start:
    if (ints[i] > ints[j]) //让第一个数与后面所有的数字比较,
    {
    temp = ints[i]; //这里利用了2个数字交换的原理,先保存一个值
    ints[i]=ints[j];//从小开始排序就是把[j]小的数赋值给[i]
    ints[j] = temp;//把大的数字给后面的[j]

    goto start;


    }

    else
    {
    if(j<ints.Length-1) //因为长度是6,j代表是的数组内的元素,最大是[5]
    {
    j++;
    goto start;
    }
    }




    }

    Console.WriteLine(); //空格


    foreach (int n in ints)//重新打印新的数字
    {
    Console.Write(n + " ");
    }


    更简单的方式:
      Array.Sort(ints); //最简单的方式,从小到大
    Array.Reverse(ints);//颠倒


    foreach (int n in ints)//重新打印新的数字
    {
    Console.Write(n + " ");
    }



  • 相关阅读:
    rmq +二分暴力 hdu 5726
    8.25 ccpc 比赛总结
    莫比乌斯反演题目总结
    HDU 4848 Wow! Such Conquering! (搜索+floyd)
    Codeforces 982 C Cut 'em all!(DFS)
    Codefoces 986C AND Graph(DFS)
    CodeForces 986A Fair(BFS)
    ACM经验贴
    Kattis A+B Problem(FFT)
    CF E. Porcelain (双向dp)
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2271590.html
Copyright © 2011-2022 走看看