
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]);
}
}
}
} 展开
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]);
}
}
}
} 展开
展开全部
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<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);
}
}
}
}
展开全部
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++;
}
}
}
}
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++;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要用数组,用集合,数据的个数是确定的,不能动态添加,二集合是可以动态添加的,使用foreach循环即可
追问
那用foreach 这个程序该如何修改呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询