数组里面都是人的名字,分割成:例如:老杨|老苏|老邹…”(老杨,老苏,老邹,老虎,老牛,老蒋,老王,老马)

来源:互联网 发布:ubuntu 14.04 iso 编辑:程序博客网 时间:2024/04/28 08:41
  //数组里面都是人的名字,分割成:例如:老杨|老苏|老邹…”(老杨,老苏,老邹,老虎,老牛,老蒋,老王,老马)

            string[] names = {"老杨","老苏","老邹","老虎","老牛","老蒋","老王","老马"};

            string str = "";
            for (int i = 0; i < names.Length-1; i++)
            {
                str = str + names[i] + "|";
            }
            str=str+names[names.Length-1];
            Console.WriteLine(str);

            Console.ReadKey();


//将一个整数数组的每一个元素进行如下的处理:如果元素是正数则将这个位置的元素的值加1,如果元素是负数则将这个位置的元素的值减1,如果元素是0,则不变。

    int[] nums = {3,5,0,-1,4,-21 };
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > 0)
                {
                    nums[i] = nums[i] + 1;
                }
                else if (nums[i] < 0)
                {
                    nums[i] = nums[i] - 1;
                }
                else
                {
                    nums[i] = nums[i]; //也可以不写
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);
            }
            Console.ReadKey();



将一个字符串数组的元素的顺序进行反转。{“我”,“是”,”好人”} {“好人”,”是”,”我”}。第i个和第length-i-1个进行换。

   string[] str = {"我","是","好人" };
            for (int i = 0; i < str.Length/2; i++)
            {
                //在循环在进行两两交换
                string temp = str[i];
                str[i] =str[str.Length - 1 - i];
                str[str.Length - 1 - i] = temp;
            }
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadKey();


     //第二种方法
            string[] str = { "我", "是", "好人" };
            for (int i = str.Length-1; i>=0; i--)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadKey();























0 0
原创粉丝点击