因此,此代码应该可以工作,但是我无法弄清楚问题所在。 以前它对我有用...但是现在我只得到0。 我知道它应该很明显,但我看不到。
namespace ConsoleApp5
{
class Program
{
private static void Main(string[] args)
{Console.WriteLine("The SmallestNumber in the given matrix is : {0}", SmallestNumber());
Console.ReadKey();
}
public static int SmallestNumber()
{
int n = 10;
int m = 10;
int[,] TempMatrix = new int[n, m];
Random r = new Random();
int smallest = TempMatrix[0,0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
TempMatrix[i, j] = r.Next(10, 3000);
if (smallest > TempMatrix[i,j])
{
smallest = TempMatrix[i, j];
}
Console.WriteLine(string.Format("{0}\t", TempMatrix[i, j]));
}
}
return smallest;
}
}
}
答案 0 :(得分:1)
初始化smallest
时,TempMatrix[0,0]
的值为0
。您生成的所有随机数都在10
和3000
之间,因此矩阵中的所有数字都大于smallest
,因为它是0
。
最初将smallest
设置为int.MaxValue
应该可以解决问题。