这项任务包括创建一个程序来读取(但不是更多)25个测试成绩,然后报告输入的成绩数量;并计算等级的算术平均值(平均值)和标准差。我不明白如何使用我创建的get方法,所以我可以在我的程序中实现它。这是我到目前为止的代码
package my.meancalculator;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class MeanCalcUI extends javax.swing.JFrame {
private double average;
private double stdDeviation;
public double[] gradeArray;
public int numElem;
public double sum;
public int i;
public double numGrades;
public MeanCalcUI() {
initComponents();
}
public double getAverage(double[] gradeArray, int numElem) {
double sum = 0;
for (int i = 0; i < numElem; i++) {
sum = sum + gradeArray[i];
}
return (sum / numElem);
}
public double getStdDev(double[] gradeArray, int numElem, double average) {
double sum = 0;
for (int i = 0; i < numElem; i++) {
sum = sum + Math.pow((gradeArray[i] - average), 2);
}
return Math.sqrt(sum / numElem);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void btnEnterGradesActionPerformed(java.awt.event.ActionEvent evt) {
gradeArray = new double[25];
JFrame frame = new JFrame();
boolean enterGrades = true;
while (enterGrades) {
try {
String gradeInput = JOptionPane.showInputDialog(frame,
"Enter Grade",
"Enter Grade",
JOptionPane.PLAIN_MESSAGE);
if ((gradeInput != null) && (gradeInput.length() > 0)) {
gradeArray[i] = Double.parseDouble(gradeInput);
average = getAverage;
numElem = numGrades + 1; //right here I know it doesn't work but i have no idea on how to make it the total of the grades entered. numElem is what the sum is getting divided by to find the average
txtNumGrades.setText((numGrades) + "");
txtMean.setText(average);
txtStdDeviation.setText(stdDeviation);
} else {
enterGrades = false;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame,
"Your input must be numeric!",
"Bad Data!",
JOptionPane.ERROR_MESSAGE);
}
}
}
另外,我不知道如果用户输入超过25个等级,我怎么能再制作一个阻挡块。以下是我老师的计划指南,以便您更好地了解它的外观和确切要求。 http://homepages.uc.edu/~thomam/OOProg_1/assignment5.html
你们可以帮助我吗?
编辑:另一个问题。如何使变量numElem工作?我不知道如何使其等于所有成绩的总和。答案 0 :(得分:2)
您要问的主要问题是尝试访问您编写的其他方法,例如getAverage()
。您需要始终传递这些方法参数;在将它作为变量处理后,没有括号的名称,但是当你将一些括号与参数放在一起时,通过语法的魔力,它就变成了一个方法调用:getAverage(gradeArray, numberOfGradesInput)
。
也就是说,有更快的方法来完成你正在做的大部分工作。
////////// CLASS FIELDS //////////
private double[] gradeArray = new double[25];
private int numberOfGradesInput = 0;
// You do not need any other fields! None! This is all you need to remember!
////////// BUTTON PRESSED //////////
if (numberOfGradesInput == 25) {
// We've already finished entering the max # of grades
JOptionPane.showMessageDialog(frame,
"You've already entered the maximum of 25 grades.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
do {
String gradeInput = JOptionPane.showInputDialog(frame,
"Enter Grade",
"Enter Grade",
JOptionPane.PLAIN_MESSAGE);
// When we receive empty/null input, we're done entering grades
if (gradeInput == null || gradeInput.length() == 0) break;
double gradeValue = 0d; // Set to avoid 'may be unset' compiler error
try {
gradeValue = Double.parseDouble(gradeInput);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame,
"Your input must be numeric!",
"Bad Data!",
JOptionPane.ERROR_MESSAGE);
continue; // start over again
}
// Put the grade into the array and update the number of grades entered
gradeArray[numberOfGradesInput] = gradeValue;
numberOfGradesInput++;
// Add to the grade total
txtNumGrades.setText(Integer.toString(numberOfGradesInput));
// ---> You should look at using a number formatter so you don't get a million digits
double gradeAverage = getAverage(gradeArray, numberOfGradesInput);
txtMean.setText(Double.toString(gradeAverage));
double standardDeviation = getStdDev(gradeArray, numberOfGradesInput, gradeAverage);
txtStdDeviation.setText(Double.toString(standardDeviation));
} while (numberOfGradesInput < 25);
此代码应该更顺畅地运行。请注意它如何跟踪numberOfGradesInput
中输入的成绩总数,并且仅在遇到空白条目或达到最大值时才循环。使用enterGrades
来跟踪您是否在循环中工作,但break
语句更快,更清晰,更易读。
我必须警告你要非常小心你的田地!您在顶部声明了一堆public
字段,然后将它们用作局部变量和循环变量。您甚至使用参数名称隐藏其中一些变量:
public int numElem;
-vs -
public double getAverage(double[] gradeArray, int numElem) {
应该避免这样做以保持代码清晰并避免错误。最好的方法是尽可能避免使用公共字段 。总的来说,如果你一次只需要一个特定方法的值,并且不需要在两次执行之间记住它,它就不应该是一个字段,更不用说公共字段了。改为在方法中使用实例变量!如果可能,只在自己的循环中创建这些变量,而不是重用它们。可以这样想:变量可以触及的距离越远,就越容易出错。一个简单的例子:
您定义了字段public int i;
。假设您在getAverage()
:for (i = 0; i < numElements; i++) {
中将其用作循环变量。现在,您还希望在名为enterGrades()
:
for (i = 0; i < 25; i++) {
getAverage(gradeArray, i);
}
每当你跳到getAverage()
时,你正在尝试使用完全相同的值来控制其他循环你是已经存在。问题将会发生!
同样,如果您不需要循环外的变量,请在其中定义它们:for (int i = 0; ...
。如果您需要知道它结束的数字,只需在循环之前定义一个局部变量。即使您没有使用它们,当您习惯于将这些名称用于循环等时,定义public int i
等字段只会引发灾难:如果您忘记定义本地变量i
,则IDE可能不会警告您使用的是public
字段。
祝你好运。
答案 1 :(得分:1)
这行代码:
average = getAverage; // from your if((gradeInput != null) && (gradeInput.length() > 0)) block;
“getAverage(args)”,average = getAverage()&lt; - 要求将参数传递给方法以便进一步操作;
将括号中的“args”替换为所需的参数,它应该有效;