2013-03-12 262 views
0

這是一個代碼,用於獲取用戶評分等級,查找它們的平均值,偏差並以表格樣式顯示此信息。我的程序需要用戶輸入並很好地顯示信息,但它不會正確計算平均值和偏差。運行時,它表示平均值爲0.我的做法與老師教我們的原因相同,但我找不到我的錯誤。計算數組的平均值

import java.util.Scanner; 

public class ClassScores{ 
public static void main(String[] args){ 
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"}; 
double[] grades = new double[7]; 
double mean=0; 
double[] difference = new double[7]; 

getScores(grades); 
average(grades, mean); 
deviation(grades,mean,difference); 
displayResults(names, grades, difference, mean); 
} 



public static double[] getScores(double[] grades) 
{ 
Scanner kb= new Scanner(System.in); 
System.out.println("Enter grades for students in alphabetical order."); 
for (int i=0;i<grades.length; i++) 
{ 
    grades[i]=kb.nextDouble(); 
} 
return grades; 
} 

public static double average(double[] grades, double mean) 
{ 
double total = 0; 
for (double i : grades) 
{ 
    total += i; 
} 
if (grades.length>0) 
{ 
    mean = total/grades.length; 
} 
return mean; 
} 

public static double[] deviation(double[] grades, double mean, double[] difference) 
{ 
for (int i=0; i<grades.length; i++) 
{ 
    difference[i]=grades[i]-mean; 
} 
return difference; 
} 

public static void displayResults(String[] names, double[] grades, double[] difference, double mean) 
{ 
System.out.println("The average score is" +mean); 
System.out.println("Student Name Grade Mean Deviation"); 

for (int i=0; i<names.length; i++) 
{ 
    System.out.printf(names[i]); 
    System.out.printf("%20f", grades[i]); 
    System.out.printf("%20f", difference[i]); 
    System.out.println(); 
} 

} 

} 

這裏是編輯好的代碼。

import java.util.Scanner;

public class ClassScores{ 
public static void main(String[] args){ 
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"}; 
double[] grades = new double[7]; 
double mean=0; 
double[] difference = new double[7]; 

getScores(grades); 
mean = average(grades); 
deviation(grades,mean,difference); 
displayResults(names, grades, difference, mean); 
} 



public static double[] getScores(double[] grades) 
{ 
Scanner kb= new Scanner(System.in); 
System.out.println("Enter grades for students in alphabetical order."); 
for (int i=0;i<grades.length; i++) 
{ 
    grades[i]=kb.nextDouble(); 
} 
return grades; 
} 

public static double average(double[] grades) 
{ 
double total = 0; 
for (double i : grades) 
{ 
    total += i; 
} 
return total/(grades.length); 
} 

public static double[] deviation(double[] grades, double mean, double[] difference) 
{ 
for (int i=0; i<grades.length; i++) 
{ 
    difference[i]=grades[i]-mean; 
} 
return difference; 
} 

public static void displayResults(String[] names, double[] grades, double[] difference, double mean) 
{ 
System.out.println("The average score is" +mean); 
System.out.println("Student Name Grade Mean Deviation"); 

for (int i=0; i<names.length; i++) 
{ 
    System.out.printf(names[i]); 
    System.out.printf("%20f", grades[i]); 
    System.out.printf("%20f", difference[i]); 
    System.out.println(); 
} 

} 

} 
+0

你可以通過添加System.out.println()語句或使用調試器來追蹤問題的位置。 – 2013-03-12 00:28:21

回答

1

首先,一般的方法應該看起來更像是這樣的:

public static double average(double[] grades) 
{ 
    // Error check up front. 
    if (grades.length == 0) { 
     throw new InvalidArgumentException("length is 0"); 
    } 

    // These next lines are good. 
    double total = 0; 
    for (double i : grades) 
    { 
     total += i; 
    } 

    // Then you can just divide and return. 
    return total/(grades.length); 
} 

沒有理由爲mean參數傳遞給average方法。這就好像將Dog傳遞給createDog方法。

但更重要的是,當您撥打average時,您不會將結果存儲在任何地方。你只是無視它。

如果你從一個指針背景的,記住的Java按值傳遞元- 改變mean,一個double,裏面的方法將會對方法之外的價值沒有影響。相反,有

double mean = average(grades); 

所以原故mean爲零是因爲你把它設置爲零,並且永遠不會改變它:

double mean = 0; 
+0

這解決了問題!非常感謝! – SMoore 2013-03-12 00:39:13

2

您從未實際使用average方法的返回結果。您可能打算將其傳遞給displayResults,或者將其分配給main()方法中的mean

0

您正在返回mean值,但您沒有將方法調用的結果分配給average任何東西。你想要的是該方法的結果賦予變量:

mean = average(grades); 

而在你average方法,你不需要採取mean作爲參數,只是宣佈在本地,並將其返回。

+0

這是問題所在,謝謝! – SMoore 2013-03-12 00:39:40

0

average(grades, mean);發送副本的變量average()方法。儘管名稱相同,average()中的mean變量與main()中的mean變量無關。爲了解決這個問題,你需要做的是這樣

mean = average(grades, mean); 

我也建議你刪除mean參數和申報average()內的局部變量。然後,你可以做

mean = average(grades); 
0

你有這樣的方法聲明爲返回一個值:

public static double average(double[] grades, double mean) 

但在你的主要方法,不使用返回結果。然後您可以打印出其他的 值「意味着」變量,它是全球性的,以該功能,並分配0

double mean=0; 
double[] difference = new double[7]; 

getScores(grades); 
**average(grades, mean);** 
deviation(grades,mean,difference); 
displayResults(names, grades, difference, mean); 

我想你想要的值:

getScores(grades); 
**mean = average(grades, mean);** 
deviation(grades,mean,difference); 
displayResults(names, grades, difference, mean);