2009-11-27 11 views
17

किसी कारण से यह कोड सरणी में उच्चतम मान के लिए तीन मान प्रिंट कर रहा है जब मैं केवल एक प्रिंट करने की कोशिश कर रहा हूं (जो 11.3 है)। क्या कोई मुझे बता सकता है कि यह क्यों कर रहा है?जावा: किसी सरणी में उच्चतम मान ढूँढना

धन्यवाद।

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() –

उत्तर

27

यह हर बार उस समय को प्रिंट करता है जो वर्तमान अधिकतम से अधिक होता है (जो आपके मामले में तीन बार होता है।) प्रिंट को लूप के बाहर ले जाएं और आपको अच्छा होना चाहिए।

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

जावा 8 के लिए, विधि 2 बहुत आसान है। –

+0

कौन सा प्रदर्शन अच्छा है? –

+0

ये भी सही और अच्छे समाधान हैं लेकिन सबसे कुशल नहीं हैं। स्वीकृत उत्तर सरणी में उच्चतम तत्व खोजने के लिए ** सबसे कुशल ** तरीका है। –

4

आप तेज और आसान तरीका है सरणियों के संबंध में विभिन्न कार्यों को करने के लिए देख रहे हैं, संग्रह वर्ग के उपयोग अत्यंत उपयोगी (प्रलेखन 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: एक सरणी सॉर्ट करने के लिए

Collections.sort(a); 

या वैकल्पिक रूप से सरणी वर्ग के उपयोग:

Arrays.sort(decMax); 

हालांकि सरणी वर्ग नहीं है

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

सरल रेखा सूची सॉर्ट करने के लिए एक विधि है जो अधिकतम मूल्य को सीधे संदर्भित करती है, इसे क्रमबद्ध करती है और अंतिम सूचकांक का जिक्र अधिकतम मूल्य है, हालांकि उपर्युक्त 2 तरीकों से सॉर्टिंग को ध्यान में रखते हुए ओ (एन लॉग एन) की जटिलता है।

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

तुम बस तत्वों के बाकी के साथ zeroth तत्व की तुलना कर रहे हैं तो यह, मूल्य पिछले सबसे बड़ा मान जो इसे शामिल होंगे प्रिंट होगा आप मामले में, एक ही समस्या हो रहा है। प्रत्येक और हर तत्व की तुलना करने के लिए हम जैसे मूल्यों स्वैप करने के लिए है:

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

आशा इस तुम्हारी मदद करेगा

संबंधित मुद्दे