我不知道我是否对此很清楚,但是我已经有了最小和最大的打印权,但是我似乎无法弄清楚如何说出它们所处的确切行和列。这就是我的意思。到目前为止
double max = m[0][0];
double min = m[0][0];
System.out.println("The matrix is : ");
for(int i = 0; i < m.length; i++)
{
for ( int j = 0; j < m[i].length; j++ )
{
System.out.printf(" " + "%6.1f " , m[i][j]);
if (m[i][j] > max)
max = m [i][j];
else if
(m[i][j] < min)
min = m [i][j];
我该如何声明他们的位置?例如:(“最大数量在第1行,第2列”)类似这样的东西……我非常感谢您的帮助
请参见以下修改。我添加了变量来跟踪最小值和最大值的索引。在你的循环结束时,你可以简单地打印出来maxIndex1
,maxIndex2
,minIndex1
,和minIndex2
。
double max = m[0][0];
double min = m[0][0];
//declare variables to track the indices of the min and max
int maxIndex1 = -1;
int maxIndex2 = -1;
int minIndex1 = -1;
int minIndex2 = -1;
System.out.println("The matrix is : ");
for(int i = 0; i < m.length; i++)
{
for ( int j = 0; j < m[i].length; j++ )
{
System.out.printf(" " + "%6.1f " , m[i][j]);
if (m[i][j] > max)
{
max = m [i][j];
//record the indices of the new max
maxIndex1 = i;
maxIndex2 = j;
}
else if (m[i][j] < min)
{
min = m [i][j];
//record the indices of the new min
minIndex1 = i;
minIndex2 = j;
}
请注意,如果您有两个相等的值并与数组中的最大值并列,则这将仅记录两个值之一。如果要记录所有领带的最小/最大位置,则可以更改此设置以保存坐标列表而不是单个坐标。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句