c#关于 遍历数组的

需要循环输入学员的名字,不能确定输入的个数,当输入为空时,按回车键即可退出输入,这个代码该如何修改?usingSystem;usingSystem.Collections... 需要循环输入学员的名字,不能确定输入的个数,当输入为空时,按回车键即可退出输入,这个代码该如何修改?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example_ForArray
{
class Program
{
static void Main(string[] args)
{
string[] inputName = new string[5];
//用for循环结合数组完成输入操作
for (int i = 0; i < inputName.Length; i++)
{
Console.WriteLine("请输入学生姓名");
string str = Console.ReadLine();
if (str == "")
{
break;
}
else
{
inputName[i] = str;
}
}
Console.Read();
//用for循环遍历数组,输出
for (int i = 0; i < inputName.Length; i++)
{
Console.WriteLine("该数组的第{0}个元素是{1}", i + 1,
inputName[i]);
}

}
}
}
展开
 我来答
msjsp
2011-09-22 · TA获得超过331个赞
知道答主
回答量:70
采纳率:0%
帮助的人:80.7万
展开全部
LZ,你可以用List<int> 来解决这个问题。 事先并不需要知道输入的个数的。到时候可以用List<int>.Count 得到输入的个数哈。

List<int> list=new List<int>();
while(true)
{
string input=Console.ReadLine();
if(string.IsNullOrEmpty(input))
{
break;
}
list.Add(input);
}
追问
对于list的应用不熟悉 。可以具体说一下吗 》在原来程序的基础上需要做出怎样的修改?
追答
using System;
using System.Collections.Generic;
using System.Text;

namespace Example_ForArray
{
class Program
{
static void Main(string[] args)
{
List list = new List();
//用for循环结合数组完成输入操作
while (true)
{
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
break;
}
list.Add(input);
}
Console.Read();
//用for循环遍历数组,输出
foreach (string item in list)
{
Console.WriteLine(item);
}

}
}
}
xiaocaia0
2011-09-22 · 超过17用户采纳过TA的回答
知道答主
回答量:71
采纳率:0%
帮助的人:50.8万
展开全部
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> name =new List<string>();
string str =string.Empty;
int i = 1;
Console.WriteLine("输入学生名字:");
str =Console.ReadLine();
while (str != "")
{
name.Add(str);
Console.WriteLine("输入学生名字:");
str = Console.ReadLine();
}
foreach (string sss in name)
{
Console.WriteLine("该数组的第"+i+"个元素是:"+sss);
i++;
}

}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
落水神
2011-09-22 · TA获得超过883个赞
知道小有建树答主
回答量:217
采纳率:0%
帮助的人:183万
展开全部
不要用数组,用集合,数据的个数是确定的,不能动态添加,二集合是可以动态添加的,使用foreach循环即可
追问
那用foreach 这个程序该如何修改呢
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yangzhenhua
2011-09-22 · TA获得超过203个赞
知道小有建树答主
回答量:781
采纳率:0%
帮助的人:353万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式