C#基础编码---打印数组

来源:互联网 发布:netty权威指南2 源码 编辑:程序博客网 时间:2024/05/08 09:27

/*
   question :打印数组
*/
using System;
namespace ArrayTest
{
    class Test
    {
           //静态方法, 是指类可以直接调用, 不用实例化.
           /*static void PrintArray(int ArrayLength)
           {
                  int[] array = new int[ArrayLength];
                  for(int i=0; i < array.Length; i++)
                  {
                         array[i]=i;
                  }
                  Console.WriteLine("Print Array's Value");
                  foreach(int i in array)
                  {
                         Console.WriteLine("array[{0}]={1}",i, array[i]);
                  }
           }*/
           //如果定义为非静态类, 在调用的时间需要实例化
           public void PrintArray(int ArrayLength)
           {
                  int[] array = new int[ArrayLength];
                  for(int i=0; i < array.Length; i++)
                  {
                         array[i]=i;
                  }
                  Console.WriteLine("Print Array's Value");
                  foreach(int i in array)
                  {
                         Console.WriteLine("array[{0}]={1}",i, array[i]);
                  }
           }
         static void Main()
         {
                int i=1;
                Test test1 = new Test(); //如果是静态类, 这句话可以不用
                while(i>0)//作用是可以多次输入
                {
                 Console.Write("Input Array's Length: ");
                 i = Int32.Parse(Console.ReadLine());
                 test1.PrintArray(i);//如果是调用的是静态类,test1.可以不用
             }
         }
    }
}

原创粉丝点击