【发布时间】:2011-02-05 12:01:59
【问题描述】:
这很好用……但是当我使用foreach 而不是for 时,这不起作用。我无法理解for 和foreach 是一样的。
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[10];
Console.WriteLine("enter the array elements to b sorted");
for(int i=0;i<10;i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
int smallest = array[0];
for(int i=0;i<10;i++)
{
if(array[i]<smallest)
{
smallest=array[i];
}
}
int largest = array[9];
for(int i=0;i<10;i++)
{
if (array[i] > largest)
{
largest = array[i];
}
}
Console.WriteLine("the smallest no is {0}", smallest);
Console.WriteLine("the largest no is {0}", largest);
Console.Read();
}
}
}
【问题讨论】:
-
向我们展示您尝试的 foreach 代码无效。你可能做错了什么
-
我很好奇你为什么说
int largest = array[9];。为什么不默认取第一个元素?
标签: c#