तो वर्तमान में मुझे "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());
}
}
'डबल [] प्रॉम्प्ट = नया डबल [args.length]; 'अगर आप (args.length! = 1) {...}' ठीक पहले चेक करते हैं तो हमेशा लंबाई 1 होगी। – pzaenger
कृपया अपना इनपुट प्रारूप परिभाषित करें। आप कहते हैं कि इनपुट युगल की एक सरणी है, फिर भी आप केवल एक कमांड लाइन तर्क होने की अनुमति देते हैं। कृपया स्पष्ट करें। –
मैंने कम से कम संख्या को कमांड लाइन से इनपुट करने की अनुमति देने के लिए एक मामूली संपादन किया है, इसलिए सीमांकन 7 – Mehmet