关于数组基础的几道例题,非常实用,知识点很多。利用数组输出数据。
第一道题:输出这三名字
string[] name = new string[] {"梅西","卡卡","郑大世" };
for (int i = 0; i < name.Length -1; i++)
{
Console.Write(name[i]+"\t");
}
Console.Write(name[name.Length -1]);
Console.ReadLine();
第二道题:将一个整数数组的每一个元素进行处理,如果元素是正数,将这个元素加一,如果元素是负数,将这个元素再减一
int[] shu=new int[]{1,3,5,-2,-3};
for (int i = 0; i < shu.Length ; i++)
{
if (shu[i]>0)
{
shu[i]++;
}
else if (shu[i]<0)
{
shu[i]--;
}
}
for (int i = 0; i < shu.Length ; i++)
{
Console.WriteLine(shu [i]);
} //小括号里定义变量,这个变量名只能在小括号内使用
//注意for 循环中i的作用域,除了这个作用域,i就不能用了
Console.ReadLine();*/
第三道题:把一组字符串类型的元素进行反转
string[] shu = new string[] {"你好","a","真的","皮球","干嘛","127" };
for (int i = 0; i < shu.Length/2; i++) //在计算机中,7/2=3,因为是整数类型
{
string temp = shu[i];
shu[i] = shu[shu.Length - 1 - i];
shu[shu.Length - 1 - i] = temp;
}
for (int i = 0; i < shu.Length ; i++)
{
Console.WriteLine(shu[i]);
}
Console.ReadLine();
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步