2014-11-02 19 views
-8

在比較的元素我有此數組:同一陣列

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

我需要的元素一起比較,並找出並輸出數量最多。我正在使用java。

+1

這是一個有趣的任務。你爲什麼覺得你必須與我們分享? – 2014-11-02 10:08:22

+1

Google搜索想法:「如何比較java中的整數」,「如何用java打印」 – 2014-11-02 10:09:40

+1

題外話,因爲這不是一個真正的問題。 – FunctionR 2014-11-02 10:24:09

回答

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; 
    } 


}