2015-12-11 6 views
5

तो वर्तमान में मुझे "Sum = 0.0" और एक मीन बराबर "NaN" मिलता है, जो कई संदेशों से लड़ने के बाद "डबल से int तक संभावित हानिकारक रूपांतरण" को चेतावनी देता है। मुझे लगता है कि कोड आखिरकार युगल ले रहा है, लेकिन फिर भी वह नहीं करता जो मैं इसे पसंद करूंगा: कमांड लाइन से मूल्य ले लो, उन्हें एक सरणी में रखें, इन्हें जोड़ दें और फिर माध्य की गणना करें।आप रकम की गणना के लिए कमांड लाइन तर्कों को एक डबल सरणी में कैसे परिवर्तित करते हैं?

कोई भी विचार जहां त्रुटियां झूठ बोलती हैं?

public class StudentMarks{ 

protected double[] marks; 
//create an array filled with double values 

public StudentMarks(double[] marks){ 
    this.marks = new double[0]; //set the default array size  
    } 

    public void setMarks(){ 
    this.marks = marks; 
    } 

    public void getArray(){ 
     //one can only print arrays using loops.. 
     //took me a little to realise that erm. 

     for(int i=0; i<marks.length; i++) 
     System.out.println(marks[i]); 
    } 

    public double calSum(){ 

    double totals = 0.0; 

    for(double i: marks) { 
     //double mLength = Double.parseDouble(marks[i]); 
     totals+= i;  
    } 
     return totals; 
    } 

    //A method to calculate the mean of all elements 

    public double calMean(){ 
     double means = (calSum()/marks.length); 
     return means; 
    } 

    //A main method to test 

    public static void main(String[] args) { 
     // Check to see if the user has actually sent a paramter to the method 
     if (args.length != 7){ 
      System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5"); 
      System.exit(-1); 
     } 

     double[] prompt = new double[args.length]; 
     for (int i =0; i<args.length; i++){ 
      prompt[i] = Double.parseDouble(args[i]); 
     } 
     StudentMarks test = new StudentMarks(prompt); 


     test.getArray(); 

     // Calculate the sum of all the values in the array and print it 
     System.out.println("Sum: "+ test.calSum()); 

     // Calculate the mean of all the values in the array and print it 
     System.out.println("Mean: "+ test.calMean()); 
    } 

} 
+1

'डबल [] प्रॉम्प्ट = नया डबल [args.length]; 'अगर आप (args.length! = 1) {...}' ठीक पहले चेक करते हैं तो हमेशा लंबाई 1 होगी। – pzaenger

+0

कृपया अपना इनपुट प्रारूप परिभाषित करें। आप कहते हैं कि इनपुट युगल की एक सरणी है, फिर भी आप केवल एक कमांड लाइन तर्क होने की अनुमति देते हैं। कृपया स्पष्ट करें। –

+0

मैंने कम से कम संख्या को कमांड लाइन से इनपुट करने की अनुमति देने के लिए एक मामूली संपादन किया है, इसलिए सीमांकन 7 – Mehmet

उत्तर

3

this.marks = new double[0]; 

उपयोग

this.marks = marks; 

आप वर्तमान में marks सदस्य चर बताए जाते हैं बल्कि पैरामीटर की तुलना में एक शून्य लंबाई सरणी होने के लिए करने के बजाय

, इसलिए तत्वों का योग शून्य है, और marks.length शून्य है, इसलिए calSum()/marks.length0.0/0.0 है, जिसे NaN माना जाता है।

+0

नहीं है आप एक उद्धारकर्ता हैं! धन्यवाद – Mehmet

+0

या कन्स्ट्रक्टर से पैरामीटर 'डबल [] अंक' हटाएं (यदि आप मानक प्रारंभिक रखना चाहते हैं) और 'setMarks() 'को' setMarks (double [] marks) में बदलें '(इस प्रकार सेटटर विधियों को काम करना चाहिए वैसे भी) और इसे अपने 'मुख्य()' विधि में उपयोग करें। –

+0

@ कोई चिंता नहीं है - अगर यह उपयोगी था तो जवाब स्वीकार करने पर विचार करें। –

0

कक्षा की शुरुआत में एक समस्या थी। कक्षा वर्तमान में 0 लंबाई सरणी में प्रारंभ की गई है। इसके बजाय आपको इसे अपने इनपुट के साथ शुरू करना चाहिए।

उपयोग

public StudentMarks(double[] marks){ 
    this.marks = marks; 
} 

public StudentMarks(double[] marks){ 
    this.marks = new double[0];  
} 

यहाँ के बजाय एक कोड के संस्करण अप तय हो गई है। स्पष्टता के लिए इनलाइन टिप्पणियों पर एक नज़र डालें।

public class StudentMarks{ 

protected double[] marks; 
//create an array filled with double values 

//Pass in the array of marks to initialize the class 
public StudentMarks(double[] marks){ 
    this.marks = marks; //set the marks array in the class to the passed in one 
} 

//Set the class marks variable to the passed in one 
public void setMarks(double[] marks){ 
    this.marks = marks; 
} 

//Change the name to "printMarks" to better reflect the purpose of the method 
public void printMarks(){ 
    //one can only print arrays using loops.. 
    //took me a little to realise that erm. 

    for(int i=0; i<marks.length; i++){ 
     System.out.println(marks[i]); 
    } 
} 

// 
public double calSum(){ 

    double totals = 0.0; 

    for(double i: marks) { 
     //double mLength = Double.parseDouble(marks[i]); 
     totals+= i;  
    } 

    return totals; 
} 

//A method to calculate the mean of all elements 
public double calMean(){ 
    double means = (calSum()/marks.length); 
    return means; 
} 

//A main method to test 
public static void main(String[] args) { 
    //Print out an error and exit only if we have less than 1 element passed in 
    if (args.length != 7){ 
     System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5"); 
     System.exit(-1); 
    } 

    double[] prompt = new double[args.length]; 
    //Note that there is no error checking here 
    for (int i =0; i<args.length; i++){ 
     prompt[i] = Double.parseDouble(args[i]); 
    } 

    //Initialize the StudentMarks class with the value of the input 
    StudentMarks test = new StudentMarks(prompt); 

    test.printMarks(); 

    // Calculate the sum of all the values in the array and print it 
    System.out.println("Sum: "+ test.calSum()); 

    // Calculate the mean of all the values in the array and print it 
    System.out.println("Mean: "+ test.calMean()); 
} 

} 
+0

"आवेदन किसी भी समय एक से अधिक तर्क पारित होने पर टूट जाएगा।" जब आपने इसे पोस्ट किया था, ओपी ने इस स्थिति को '! = 7' में अपडेट किया था; ऐसा लगता है कि ओपी पर एक बहुत ही विशिष्ट आवश्यकता है। –

+0

टिप @ एंडी टर्नर के लिए धन्यवाद। उस समय मैंने कोड लिखा था, ओपी में अभी भी '! = 1' है, इसलिए मैंने एक धारणा की, और इसे बदल दिया। हालांकि आप सही हैं, ऐसा लगता है कि उनके पास एक विशिष्ट आवश्यकता है। मैंने मिलान करने के लिए अपना कोड संपादित कर लिया है। –

+0

"उपरोक्त कोड के साथ वास्तव में कुछ मुद्दे थे।" क्या यह अब सच है कि आपने 'लंबाई <1' के बारे में बात हटा दी है? मैं कोड के बड़े डंप पोस्ट करने से बचने के लिए प्रवृत्त हूं क्योंकि यह बहुत स्पष्ट नहीं है कि (और क्यों) आपने उस कन्स्ट्रक्टर के अलावा अर्थात् बदल दिया है (आपको परिवर्तन देखने के लिए पृष्ठ को ऊपर और नीचे स्क्रॉल करना है, बहुत आसान नहीं)। यह मत भूलना कि आपके लिए स्पष्ट चीजें दूसरों को पढ़ने के लिए जरूरी नहीं हैं। –

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