본문 바로가기

호그와트

TwoArray.java

728x90

import java.util.Scanner;

public class TwoArrays {
    public static int findSmallest(int[][] myArray) {
        int result = myArray[0][0];
        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray[i].length; j++) {
                if (result > myArray[i][j]) {
                    result = myArray[i][j];
                }
            }
        }
        return result;
    }
    public static int findBiggest(int[][] myArray) {
        int result = myArray[0][0];
        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray[i].length; j++) {
                if (result < myArray[i][j]) {
                    result = myArray[i][j];
                }
            }
        }
        return result;
    }

    public static int[][] createArray(int k, Scanner user_input) {
        int[][] four = new int[k][];
        for (int i = 0; i < k; i++) {
            four[i] = new int[i + 1];
            for (int j = 0; j <= i; j++) {
                four[i][j] = user_input.nextInt();
            }
        }
        return four;
    }

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("Enter the number of rows for the triangle array:");
        int k = user_input.nextInt();

        int[][] myArray = createArray(k, user_input);
        int smallest = findSmallest(myArray);
        int biggest = findBiggest(myArray);

        System.out.println("The smallest number in the array is: " + smallest);

        user_input.close();
    }

}
728x90