2009-11-27 241 views
17

出于某种原因,当我试图打印一个(11.3)时,此代码正在打印数组中最高值的三个值。有人可以向我解释为什么这样做吗?Java:查找数组中的最大值

谢谢。

import java.util.Scanner; 

public class Slide24 
{ 
    public static void main (String [] args) 
    { 
     Scanner in = new Scanner(System.in); 

     double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4, 
      8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2, 
      3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1}; 

     double total = 0, avgMax = 0; 

     for (int counter = 0; counter < decMax.length; counter++) 
     { 
     total += decMax[counter]; 
     } 

     avgMax = total/decMax.length; 

     System.out.printf("%s %2.2f\n", "The average maximum temperature for December was: ", avgMax); 

     //finds the highest value 
     double max = decMax[0]; 

     for (int counter = 1; counter < decMax.length; counter++) 
     { 
     if (decMax[counter] > max) 
     { 
      max = decMax[counter]; 
      System.out.println("The highest maximum for the December is: " + max); 
     } 

     }   
    } 
} 
+1

Collections.max(ArrayList的).toInt() – 2017-12-05 13:33:50

回答

27

每次找到一个高于当前最大值的数字(在您的情况下会发生三次)。将该打印移到for循环的外部,您应该很好。

for (int counter = 1; counter < decMax.length; counter++) 
{ 
    if (decMax[counter] > max) 
    { 
     max = decMax[counter]; 
    } 
} 

System.out.println("The highest maximum for the December is: " + max); 
6

您需要后打印出来的最大你扫描所有的人:

for (int counter = 1; counter < decMax.length; counter++) 
{ 
    if (decMax[counter] > max) 
    { 
     max = decMax[counter]; 
     // not here: System.out.println("The highest maximum for the December is: " + max); 
    } 
} 
System.out.println("The highest maximum for the December is: " + max); 
1

你有你的打印()for()循环语句,它应该是后所以它只打印一次。它目前的方式是,每当最大更改打印max

17

要从数组中找出最高值(最大值)或最小值(最小值),这可以为您提供正确的方向。以下是从原始数组中获取最高值的示例代码。

方法1:

public int maxValue(int array[]){ 
    List<Integer> list = new ArrayList<Integer>(); 
    for (int i = 0; i < array.length; i++) { 
    list.add(array[i]); 
    } 
return Collections.max(list); 

}

得到最低值,你可以使用

Collections.min(list)

方法2:

public int maxValue(int array[]){ 
    int max = Arrays.stream(array).max().getAsInt(); 
    return max; 
} 

下面的配置应该工作。

System.out.println("The highest maximum for the December is: " + maxValue(decMax));
+0

对于Java 8,方法2是非常方便的。 – 2017-01-02 07:50:45

+0

哪一个性能好? – 2017-02-03 09:21:47

+0

这些也是正确的,很好的解决方案,但不是最有效的。接受的答案是**最有效的**方法来查找数组中的最高元素。 – 2017-02-03 11:12:35

4

如果您正在寻找执行的问候阵列的各种动作最快,最简单的方法,使用Collections类的是非常有用的(可从https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html文档),行动从发现的最大,最小范围,排序,相反的顺序等

的简单方式找到与使用类别阵列的最大值:

Double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4, 8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2, 3.5, 3.0, 1.1, 6.5, 5.1, -1.2, -5.1, 2.0, 5.2, 2.1}; 
List<Double> a = new ArrayList<Double>(Arrays.asList(decMax)); 
System.out.println("The highest maximum for the December is: " + Collections.max(a)); 

如果有兴趣在寻找最小值,类似于发现米aximum:

System.out.println(Collections.min(a)); 

最简单的线对列表进行排序:但是Arrays类不

Arrays.sort(decMax); 

Collections.sort(a); 

或可选择地使用Arrays类的排序数组有一个直接引用最大值的方法,对它进行排序并引用最后一个索引是最大值,但记住用上述2种方法排序的复杂度为O(n log n)。

5

同其他人的建议,只提做的更清洁的方式:

int max = decMax[0]; 
for(int i=1;i<decMax.length;i++) 
    max = Math.max(decMax[i],max); 
System.out.println("The Maximum value is : " + max); 
0

较短的解决方案,具有阵列的最大值:

double max = Arrays.stream(decMax).max(Double::compareTo).get(); 
0

您可以使用一个函数,接受一个数组并在其中找到最大值。我做到了通用的,所以它也可以接受其他数据类型

public static <T extends Comparable<T>> T findMax(T[] array){  
    T max = array[0]; 
    for(T data: array){ 
     if(data.compareTo(max)>0) 
      max =data;     
    } 
    return max; 
} 
0

你可以这样写。

import java.util.Scanner; 
class BigNoArray{ 

    public static void main(String[] args){ 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter how many array element"); 
     int n=sc.nextInt(); 
     int[] ar= new int[n]; 
     System.out.println("enter "+n+" values"); 
     for(int i=0;i<ar.length;i++){ 
      ar[i]=sc.nextInt(); 
     } 
     int fbig=ar[0]; 
     int sbig=ar[1]; 
     int tbig=ar[3]; 
      for(int i=1;i<ar.length;i++){ 
       if(fbig<ar[i]){ 
        sbig=fbig; 
        fbig=ar[i]; 
       } 
       else if(sbig<ar[i]&&ar[i]!=fbig){ 
        sbig=ar[i]; 
       } 
       else if(tbig<ar[i]&&ar[i]!=fbig){ 
        tbig=ar[i]; 
       } 
      } 
     System.out.println("first big number is "+fbig); 
     System.out.println("second big number is "+sbig); 
     System.out.println("third big number is "+tbig); 
    } 
} 
0
void FindMax() 
{ 
    int lessonNum; 

    System.out.print("Enter your lesson numbers : "); 
    lessonNum = input.nextInt(); 
    int[] numbers = new int[lessonNum]; 
    for (int i = 0; i < numbers.length; i++) 
    { 
     System.out.print("Please enter " + (i + 1) + " number : "); 
     numbers[i] = input.nextInt(); 
    } 
    double max = numbers[0]; 
    for (int i = 1; i < numbers.length; i++) 
    { 
     if (numbers[i] > max) 
     { 
      max = numbers[i]; 
     } 
    } 
    System.out.println("Maximum number is : " + max); 
} 
0

你只是用比较的元素的其余零个元素,因此会打印出值最大的最后的值,它会包含在你的情况下,同样的问题发生。为了比较每一个元素,我们必须像交换价值:

double max = decMax[0]; 
    for (int counter = 1; counter < decMax.length; counter++){ 
     if(max<decMax[i]){ 
      max=decMax[i]; //swapping 
      decMax[i]=decMax[0]; 
     } 
    } 
    System.out.println("The max value is "+ max); 

希望这将帮助你