1.将一个具有5个元素的数组中的数据逆序,并输出原始数组中的数据及逆序后数组中的数据。

1.将一个具有5个元素的数组中的数据逆序,并输出原始数组中的数据及逆序后数组中的数据。 有人会编吗?

//5个数据

            string[] arr = new string[] { "1", "2", "3", "4", "5" }; //可以任何类型

            for (int i = arr.Length-1; i >= 0; i--)

            {

                Console.Write(arr[i] + "\t");  //数据逆序

            }

            Console.WriteLine();

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write(arr[i] + "\t");  //原始数组

            }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-13
首先先声明一个数组,然后使用for循环进行输出,具体代码如下:
int[] arr=new int[5];
int[] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i;//给数组赋值
Response.Write(arr[i]+"    ");
//输出数组的值
}
for (int i = arr.Length-1; i >= 0; i--)
{
Response.Write(arr[i]+"    ");
//反序输出数组的值
}
第2个回答  2010-07-13
前一段给人写的Int数组反序,你当参考吧

List<int> SortList = new List<int>();
bool EndInput = false;
while (!EndInput)
{
Console.WriteLine("请输入一个数字,按回车确定.结束输入请输入\"end\"");
string InputString = Console.ReadLine();
if (string.Equals(InputString, "end", StringComparison.CurrentCultureIgnoreCase))
{
EndInput = true;
}
else
{
try
{
SortList.Add(Convert.ToInt32(InputString));
}
catch (FormatException fx)
{
Console.WriteLine("输入数字的格式有误!详细错误信息:" + fx.Message);
}
catch (OverflowException oe)
{
Console.WriteLine("输入数字超出Int32的范围!详细错误信息:" + oe.Message);
}
}
}

Console.WriteLine("开始反序输出数字,按输入数字组反序输出:");
for (int i = SortList.Count; i > 0; i--)
{
Console.WriteLine(SortList[i - 1].ToString());
}
Console.WriteLine("按输入单个数反序输出:");
for (int i = SortList.Count; i > 0; i--)
{
Console.Write(SortList[i - 1].ToString().Reverse().ToArray());
}
Console.WriteLine("输出数字完毕,请按任意键退出");
Console.ReadKey();
第3个回答  2010-07-12
用FOR循环
先数组赋值
例:for i=1 to 5
j(i)=i;
next i
for i=5 to 1 step -1
print j(i);
next i;
这样就可以了 格式可能不符合 大致意思是这样
第4个回答  2010-07-27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clock
{
class Program
{

static void Main(string[] args)
{
int[] i = new int[5] { 1, 2, 3, 4, 5 }; //定义数组
foreach (int j in i) //遍历数组
Console.WriteLine(j);
Console.WriteLine("----------------邪恶分割线-----------------");
Array.Reverse(i); //数组的反转方法
foreach (int j in i)
Console.WriteLine(j);
}
}
}
相似回答