2014-07-11 50 views
-3

在Java中的我自學的數量最多,我想從每個2個陣列獲得數量最多的充分利用各2列

我不知道爲什麼下面的輸出被執行:

Largest: 3 
Largest: 8 
Largest: 9 
Largest: 23 
Largest: 33 
Largest: 41 
Largest: 51 

正確的輸出應該是:

Largest: 23 
Largest: 51 

下面的代碼如何能得到最多?因爲(getLargest1[i] > largest)

等於getLargest1[i] > 0,其中largest = 0

爲什麼要largest = getLargest1[i];


public class MyProgram 
{ 
    public void start() 
    { 
     int[] getLargest1 = {3, 8, 4, 9, 5, 5, 23, 14}; 
     int[] getLargest2 = {33, 23, 41, 9, 17, 51, 23, 45}; 
     getLargestFunc(getLargest1, getLargest2); 
    } 

    private void getLargestFunc(int[] getLargest1, int[] getLargest2) 
    { 
     int largest = 0; 

     for (int i = 0; i < getLargest1.length; i++) 
     { 
      if (getLargest1[i] > largest) 
      { 
       largest = getLargest1[i]; 
       System.out.println("Largest: " + largest); 
      } 
     } 

     for (int i = 0; i < getLargest2.length; i++) 
     { 
      if (getLargest2[i] > largest) 
      { 
       largest = getLargest2[i]; 
       System.out.println("Largest: " + largest); 
      } 
     } 
    } 
} 
+1

將您的'print' stmt移到for循環之外。 – Ambrish

+0

在for循環後移動每個打印語句,並且在第一個循環後將「最大」變量重置爲「0」# – BackSlash

+0

@ BackSlash或每個陣列的第一個元素 –

回答

2

你必須移動for環路外的

System.out.println("Largest: " + largest); 

語句。喜歡的東西:

for (int i = 0; i < getLargest2.length; i++) 
{ 
    if (getLargest2[i] > largest) 
    { 
     largest = getLargest2[i]; 
     } 
} 
System.out.println("Largest: " + largest); 

當然,largest變量的第一個循環後復位。

+0

謝謝@BackSlash。補充說。 :) –

3

可以這樣做太

int[] getLargest1 = {3, 8, 4, 9, 5, 5, 23, 14}; 
    int[] getLargest2 = {33, 23, 41, 9, 17, 51, 23, 45}; 
    Arrays.sort(getLargest1); 
    Arrays.sort(getLargest2); 

System.out.println("Largest values " + getLargest1[getLargest1.length-1] 
            +" and "+getLargest2[getLargest2.length-1]); 

輸出:

Largest values 23 and 51 
0
for (int i = 0; i < getLargest1.length; i++) 
{ 
    if (getLargest1[i] > largest) 
    { 
     largest = getLargest1[i]; 
    } 
} 

System.out.println("Largest: " + largest); 

print的說法應該是圈外的,或者你只是通過在所有的變化迭代最大。這就是爲什麼它會給你帶來奇怪的輸出。


我只是做一個方法,可以用於這兩個數組。這使得更有用的方法。

public void start() 
{ 
    int[] getLargest1 = {3, 8, 4, 9, 5, 5, 23, 14}; 
    int[] getLargest2 = {33, 23, 41, 9, 17, 51, 23, 45}; 
    System.out.println(getLargestFunc(getLargest1)); 
    System.out.println(getLargestFunc(getLargest2)); 
} 

private int getLargestFunc(int[] array){ 
    if(array == null) return null; 
    largest = array[0]; 
    for(int i = 0; i < array.length; i++){ 
     if(array[i] > largest){ 
      largest = array[i]; 
     } 
    } 
    return largest; 
} 
0

你在錯誤的地方打印您應該打印您的循環之外:

for (int i = 0; i < getLargest1.length; i++) 
     { 
      if (getLargest1[i] > largest) 
      { 
       largest = getLargest1[i]; 

      } 
     } 
     System.out.println("Largest: " + largest); 

     largest = 0; 

     for (int i = 0; i < getLargest2.length; i++) 
     { 
      if (getLargest2[i] > largest) 
      { 
       largest = getLargest2[i]; 
      } 
     } 

     System.out.println("Largest: " + largest); 
0

我認爲你必須把循環操作以外的println系統..

+0

'最大'也需要重置。 – Unihedron

0

另一種方式是 - ArrayUtils(org.apache.commons.lang3.ArrayUtils)和Collections

int[] getLargest1 = {3, 8, 4, 9, 5, 5, 23, 14}; 
    int[] getLargest2 = {33, 23, 41, 9, 17, 51, 23, 45}; 


    List a = Arrays.asList(ArrayUtils.toObject(getLargest1)); 
    System.out.println("Largest :"+Collections.max(a)); 

    List b = Arrays.asList(ArrayUtils.toObject(getLargest2)); 
    System.out.println("Largest :"+Collections.max(b)); 
+0

它給出'[I @ 5483cd'沒有'ArrayUtils.toObject(arr)'而不是'23' –

0

嘗試(未測試):

final int largest1 = IntStream.of(getLargest1).max().getAsInt(); 
final int largest2 = IntStream.of(getLargest2).max().getAsInt(); 

return largest1 >= largest2 ? largest1 : largest2;