본문 바로가기

JavaCode(review)

ArrayChallenge / console값 입력 후 최소값 구하기.

반응형
package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class Main {             //console값 입력 후 최소값 print

    private static Scanner scanner = new Scanner(System.in);    //console search

    public static void main(String[] args) {
        int[] arrayInteger= readIntegers(8);
        printArray(arrayInteger);
        System.out.println("Min = "+findMin(arrayInteger));
    }

    private static int[] readIntegers(int capacity){      //capacity->array
        int[] array = new int[capacity];
        System.out.println("Enter "+capacity+"integer values.\r");
        for(int i=0; i<array.length; i++){
            array[i]=scanner.nextInt();         //int값console에 입력가능토록
        }return array;
    }

    //print Input array
    private static void printArray(int[]array){
        for(int i=0; i<array.length; i++){
            System.out.println("Element "+i+ " = "+array[i]);
        }
    }

    //findMin

    private static int findMin(int[] array){
        int Min=Integer.MAX_VALUE;
        for(int i=0; i<array.length;i++){     //큰순서대로
                if(array[i]<Min) {
                   Min = array[i];
                }
            }
        return Min;     //
    }
}

Output

D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=49992:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\MinElementChallenge\out\production\MinElementChallenge com.company.Main
Enter 8integer values.
34
5
23
2
1
3
5
6
Element 0 = 34
Element 1 = 5
Element 2 = 23
Element 3 = 2
Element 4 = 1
Element 5 = 3
Element 6 = 5
Element 7 = 6
Min = 1

Process finished with exit code 0