二维表的最大/最小值

时间:2015-12-16 13:54:06

标签: java

我必须在java中创建一个表,而不是使用方法fin max min value。这是我的代码,我不知道如何输入一个可以返回最大/最小值的方法。

import java.io.*;
public class Max {

public static void main(String[] args)throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader   (System.in));

    System.out.print("Input a numbers of rows: ");
    int a = Integer.parseInt(input.readLine());

    System.out.print("Input a numbers of a columns: ");
    int b = Integer.parseInt(input.readLine());

    int [][] tab = new int [a][b];

    for (int i = 0; i < tab.length; i++){
        for (int j = 0; j < tab[i].length; j++){
            tab[i][j] = (int)(Math.random()*10001);
        }
    }
    for (int i = 0; i < tab.length; i++){
        for (int j = 0; j < tab[i].length; j++){
            System.out.print(tab[i][j] + " ");
        }
        System.out.println();
    }
   }
 }                     

2 个答案:

答案 0 :(得分:0)

在设置数组时

int max = 0;
for (int i =0; i < tab.length; i++) {
    for (int j = 0; j < tab[i].length; j++) {
        int val = Math.random(100001);
        if (val > max) {
            max = val;
        }
    }
}

或设置完阵列后

int max = 0;
for (int i =0; i < tab.length; i++) {
    for (int j = 0; j < tab[i].length; j++) {
        if (tab[i][j] > max) {
            max = tab[i][j];
        }
    }
}    

答案 1 :(得分:0)

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < tab.length; i++) {
            for (int j = 0; j < tab[i].length; j++) {

                if (tab[i][j] < min) {
                    min = tab[i][j];
                }
                if (tab[i][j] > max) {
                    max = tab[i][j];
                }
            }

        }
System.out.println("min:" + min + ", max:" + max);