2 回答
TA贡献1155条经验 获得超0个赞
尝试这个 :
public class Average {
double numberOfGradeOne, numberOfGradeTwo, numberOfGradeThree, numberOfGradeFour, numberOfGradeFive;
double numberOfGrades, gradeSum, gradeAverage;
public Average(double numberOfGradeOne, double numberOfGradeTwo, double numberOfGradeThree, double numberOfGradeFour, double numberOfGradeFive, double numberOfGrades) {
this.numberOfGradeOne = numberOfGradeOne;
this.numberOfGradeTwo = numberOfGradeTwo;
this.numberOfGradeThree = numberOfGradeThree;
this.numberOfGradeFour = numberOfGradeFour;
this.numberOfGradeFive = numberOfGradeFive;
this.numberOfGrades = numberOfGrades;
calculateAverage();
}
void calculateAverage () {
numberOfGrades=numberOfGradeFive+numberOfGradeFour+numberOfGradeThree+numberOfGradeTwo+numberOfGradeOne;
gradeSum =5*numberOfGradeFive+4*numberOfGradeFour+3*numberOfGradeThree+2*numberOfGradeTwo+numberOfGradeOne;
gradeAverage=gradeSum/numberOfGrades;
}
public double getGradeAverage() {
return gradeAverage;
}
}
在你的片段中:
average = new Average(12,34,54,12,44.6,5);
Log.v("Average",""+average.getGradeAverage());
textView.setText(average.getGradeAverage()+" ");
或者你甚至可以评论 calculateAverage(); 从构造函数并像这样调用:
average = new Average(12,34,54,12,44.6,5);
average.calculateAverage();
Log.v("Average",""+average.getGradeAverage());
textView.setText(average.getGradeAverage()+" ");
TA贡献1777条经验 获得超10个赞
您应该尝试通过调用来使用static methodthen 调用Average.calculate(gradeOne, gradeTwo, gradeThree, gradeFour, gradeFive)。class应该看起来像这样。
import java.util.Arrays;
import java.util.OptionalDouble;
import java.util.concurrent.atomic.AtomicReference;
public class Average {
public static double calculate(double... inputs) {
AtomicReference<Double> multiplier = new AtomicReference<>((double) 0);
OptionalDouble average = Arrays.stream(inputs)
.map(value -> {
multiplier.set((multiplier.get() + 1));
return value * multiplier.get();
})
.average();
return average.isPresent() ? average.getAsDouble() : 0;
}
}
添加回答
举报