黑马程序员_C#学习之数组(代码)

来源:互联网 发布:adobe系列软件下载 编辑:程序博客网 时间:2024/04/29 19:59
 

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

using System;using System.Collections.Generic;using System.Text;namespace Ex7_6{    class Program    {        static void Main(string[] args)        {           //C#二位数组转置输出            int[,] OriginalArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 },                                 { 10, 11, 12 }, { 13, 14, 15 } };            int[,] ReversionArray=new int[3,5];            for (int i = 0; i <= OriginalArray.GetUpperBound(0); i++)            {                for (int j = 0; j <= OriginalArray.GetUpperBound(1); j++)                {                    ReversionArray[j, i] = OriginalArray[i, j];                }            }            Console.WriteLine("原始的数组为:");            PrintArray(OriginalArray);            Console.WriteLine("转置后的数组为:");            PrintArray(ReversionArray);      Console.ReadLine();        }        public static void PrintArray(int[,] intArray)        {            for (int i = 0; i <= intArray.GetUpperBound(0); i++)            {                for (int j = 0; j <=intArray.GetUpperBound(1); j++)                {                    Console.Write(intArray[i, j] + " ");                }                Console.WriteLine();            }        }    }}


 

    

 

using System;using System.Collections.Generic;using System.Text;namespace text_{    class Program    {        public static int a;        public static void OneString(string s)        {            //C#输入一串字符串求单词个数            string[] thisString = s.Split(new char[] { ' ' });            a = thisString.Length;           //测试有无多个连续空格代码           // for (int i=0; i < thisString.Length; i++)           // {           //     Console.WriteLine("qq*"+thisString[i]+"*qq");           // }            for (int i = 0; i < thisString.Length; i++)              {                 if (thisString[i] == "")                     a--;              }            Console.WriteLine("此字符串中{0}个单词!", a);        }        static void Main(string[] args)        {             //string onestring = "   a b  c  d e  f g h  ";            Console.WriteLine("输入一个包含单词的字符串:");            string onestring = Console.ReadLine();            Console.WriteLine(onestring);                        OneString(onestring);            Console.Read();        }    }}


 

   int[] a = new int[12];            Random ran = new Random();            for (int i = 0; i < a.Length; i++)            {            one:                           a[i] =(int)ran.Next()+1;                for(int j=0;j<i;j++)                {                    if(a[i]==a[j])                        goto one;                }            }            foreach (int n in a)                Console.Write(n + " ");           int temp;           int m = 0;            for (int j = 0; j<a.Length; j++)            {                temp = j;                for(int i=j+1;i<a.Length;i++ )                {                    if (a[i] < a[temp])                        temp = i;                }                if(temp!=j)                    m = a[j];                    a[j] = a[temp];                    a[temp] = m;            }            Console.WriteLine();            Console.WriteLine("输出排序后的结果:");            foreach (int n in a)                Console.Write(n + " ");

 

 

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://net.itheima.com/

原创粉丝点击