比较同一数组中的元素

时间:2014-11-02 10:06:26

标签: java

我有这个数组:

int[] times ={ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265, 345, 360, 423 };

我需要将元素进行比较,找到并输出最大的数字。我正在使用java。

1 个答案:

答案 0 :(得分:1)

我正在帮助你,因为你纯粹是编程的新手 参考official java tutorials
不要期望每次都有这种帮助:)

public class FindMax {

    public static void main(String[] args) {
        int[] times ={ 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265, 345, 360, 423 };
        System.out.println("Max Element: "+ findMax(times));
    }
    //method which takes input as an array of integers and returns max integer
    public static int findMax(int[] nums){
        int max=nums[0];
        for(int num :nums){
            if(max<num){
                max=num;
            }
        }
        return max;
    }


}
相关问题