在输入有效成绩之前,如何继续提示错误消息?现在,我只提示4次,然后显示平均值。
另外,我如何计算仅有三个最高等级的平均值?
final int MAX_NUM_GRADES = 4;
double avg;
double sum = 0;
int count = 1;
double[] examGrade = new double[MAX_NUM_GRADES];
for (int i = 0; i < examGrade.length; i++) {
try {
examGrade[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter Grade " + count + ":"));
if (examGrade[i] < 0 || examGrade[i] > 100) {
JOptionPane.showMessageDialog(null,"ERROR!.");
}
else {
count++;
sum = sum + examGrade[i];
}
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"ERROR!");
}
}
avg = sum/examGrade.length;
JOptionPane.showMessageDialog(null,"average exam grade is: "
+ String.format("%.1f",avg));
答案 0 :(得分:1)
这是因为您的for
循环仅通过examGrade
数组的长度进行迭代,该数组在程序开始时使用MAX_NUM_GRADES
设置为4。
相反,您可以尝试跟踪有效答案,只计算一次使用while-loop
回答3个有效成绩。下面使用的TreeSet
会自动对输入到集合中的值进行排序,因此会为您完成排序。
像这样:
TreeSet<Double> grades = new TreeSet<Double>();
final int MAX_NUM_GRADES = 4;
double avg;
double sum = 0;
double temp = 0d;
while(grades.size() < MAX_NUM_GRADES){
temp = Double.parseDouble(JOptionPane.showInputDialog("Enter Grade " + count + ":"));
if (examGrade[i] < 0 || examGrade[i] > 100) {
JOptionPane.showMessageDialog(null,"ERROR!.");
} else {
grades.add(temp);
}
}
for (Double val : grades) {
sum += val;
}
avg = sum/grades.size();
JOptionPane.showMessageDialog(null,"average exam grade is: "
+ String.format("%.1f",avg));
只需将try-catch
块添加到您想要的位置即可。
答案 1 :(得分:1)
我建议把它放在循环中。下面是一个psudo代码示例。
for(int I = 0; I < examGrade.length; I++) {
invalid = true;
while(invalid) {
//Accept the input, set invalid = false if it is correct
}
}
答案 2 :(得分:1)
你需要一个while
或一个do while
循环,你一直循环直到输入有效
final int MAX_NUM_GRADES = 4;
double avg;
double sum = 0;
int count = 1;
double[] examGrade = new double[MAX_NUM_GRADES];
boolean valid; // this is true when the input is valid
for (int i = 0; i < examGrade.length; i++) {
valid=false;// initialize valid
try {
do{
examGrade[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter Grade " + count + ":"));
if (examGrade[i] < 0 || examGrade[i] > 100) {
JOptionPane.showMessageDialog(null,"ERROR!.");
valid=false;//in this case input is not valid
}
else {
count++;
sum = sum + examGrade[i];
valid=true;// input is valid
}
}while(!valid)//keep looping while valid is false
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"ERROR!");
}
}
avg = sum/examGrade.length;
JOptionPane.showMessageDialog(null,"average exam grade is: "
+ String.format("%.1f",avg));
现在计算你需要对数组进行排序的3个最高等级的平均值,然后选择最后3个等级,你可以使用任何排序算法,例如:
int tmp;
for(int i=0;i<examGrade.length;i++){
for(int j=i;j<examGrade.length;j++{
if(examGrade[j]<examGrade[i]){
tmp=examGrade[j];
examGrade[j]=examGrade[i];
examGrade[i]=tmp;
}
}
//now that its sorted you can calculate the average of highest scores
float avg=0;
for(int i=1;i<examGrade.length;i++){
avg+=examGrade[i];
}
avg=avg/3;
答案 3 :(得分:1)
将您的代码更改为:
final int MAX_NUM_GRADES = 4;
double avg;
double sum = 0;
double min = Double.MAX_VALUE;
int count = 1;
double[] examGrade = new double[MAX_NUM_GRADES];
for (int i = 0; i < examGrade.length; i++) {
boolean invalid = true;
while(invalid) {
try {
examGrade[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter Grade " + count + ":"));
if (examGrade[i] < 0 || examGrade[i] > 100) {
JOptionPane.showMessageDialog(null,"ERROR!.");
}
else {
invalid = false;
count++;
sum = sum + examGrade[i];
if(examGrade[i] < min) min = examGrade[i];
}
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"ERROR!");
}
}
}
avg = (sum - min)/(examGrade.length - 1);
JOptionPane.showMessageDialog(null,"average exam grade is: "
+ String.format("%.1f",avg));