在“QZKS”中添加一个文件名为“test3.cs“的文件,在此文件中编写一个类,类的功能为:
(1)将1、2、3、4、5五个数依次存到一个一维数组中。
(2)使用foreach语句依次输出各元素的值。
在“QZKS”中添加一个文件名为“test3.cs“的文件,在此文件中编写一个类,类的功能为:
(1)将1、2、3、4、5五个数依次存到一个一维数组中。
(2)使用foreach语句依次输出各元素的值。
右键添加文件--选类--输入名称为test3
然后打开你新加的这个类名,
输入代码
public string[] str=new string[5];
public void addNums()
{
int i;
for(i=0;i<4;i++)
{
str[i]=i.tostring();
}
}
public void printNums()
{
foreach(string s in str)
{
console.writeline(s);
}
}
应该是这样,我没有调试。
IList<int> list=new list<int>();
for(int i=1;i<6;i++){
list.Add(i);
}
foreach (int item in list)
Console.WriteLine(item);
int[] arr=new int[]{1,2,3,4,5};
for(int i=0;i<arr.Length;i++)
{
console.WriteLine(arr[i]);
}
这样就可以了是不是很简单啊,楼上的太繁琐啦
欢迎入团
byte[] a = new byte[5];
for (int i = 1;i <= 5;i++)
{a[i - 1] = i;}
foreach (byte b in a)
Console.WriteLine(b);