[Java] Output the maximum value from an array using Java standard output

less than 1 minute read

Java exercises on a site. A memo of a program that inputs the integer n on standard input, then inputs any number of n and outputs the maximum value.

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
       // enter integer n
       int num = sc.nextInt();
       int max = 0;
       int[] array = new int[num];

       //Enter any number n times
       for(int i = 0; i <num; i++){
          array[i] = sc.nextInt();
          if(max <array[i]){
             max = array[i];
          }
       }
       System.out.println(max);
    }
}