Find Max Method


Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem types
Allowed languages
Java 19, Java 8

題目說明

請設計一副程式,找出5個數的最大值後回傳答案

輸入限制:整數

輸入

五個整數

輸出

最大值

測試資料0 輸入

1 2 3 4 5

測試資料0 輸出

5

Comments


  • 0
    scu09156146  commented on May 2, 2024, 10:44 a.m.

    題解

    import java.util.Scanner;
    
    public class FindMaxMethod {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            int[] array = new int[5];
            for (int i = 0; i < 5; i++) {
                array[i] = input.nextInt();
            }
            input.close();
    
            System.out.print(findMax(array)); // 呼叫副程式,印出回傳值
    
        }
    
        public static int findMax(int[] array) {
            int max = Integer.MIN_VALUE;
            // 【注意】陣列中可能皆為負數,最大值不可預設為0
            for (int i = 0; i < 5; i++) {
                if (max < array[i])
                    max = array[i];
            }
            return max;
        }
    
    }