计算平均成绩

时间:2015-07-06 21:34:40

标签: java

我正在为自己制作一个小程序来计算我的平均分数。

现在,我有一个完全有效的计划,但效率很低,而且非常有限。代码:

import java.util.Scanner;
public class GradeCalculator {

public static void main(String[] args) {

Scanner count = new Scanner(System.in);
    int counter = count.nextInt();

        Scanner input = new Scanner(System.in);

    double mark = input.nextDouble();
    double promark = (mark/100);
    double weight = input.nextDouble();
    double total = (promark*weight);
    double finalmark = (total);
    if (counter==1){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark);
    System.exit(0);
    }

    double mark1 = input.nextDouble();
    double promark1 = (mark1/100);
    double weight1 =input.nextDouble();
    double total1 = (promark1*weight1);
    double finalmark1 = (total+total1);
    if (counter==2){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark1);
    System.exit(0);
    }

    double mark2 = input.nextDouble();
    double promark2 = (mark2/100);
    double weight2 =input.nextDouble();
    double total2 = (promark2*weight2);
    double finalmark2 = (total+total1+total2);
    if (counter==3){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark2);
    System.exit(0);
    }


    double mark3 = input.nextDouble();
    double promark3 = (mark3/100);
    double weight3 =input.nextDouble();
    double total3 = (promark3*weight3);
    double finalmark3 = (total+total1+total2+total3);
    if (counter==4){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark3);
    System.exit(0);
    }

    double mark4 = input.nextDouble();
    double promark4 = (mark4/100);
    double weight4 = input.nextDouble();
    double total4 = (promark4*weight4);
    double finalmark4 = (total+total1+total2+total3+total4);
    if(counter==5){
    System.out.println("The average mark for your "+counter+" assignments/exams is "+finalmark4);
    System.exit(0);
    }

因此,该程序首先要求用户提供他们所拥有的不同标记的数量。

然后用户将输入标记和该工作的加权百分比。

之后,程序将计算所有标记的平均标记,并考虑每个标记的百分比。

该计划基本上是本网站的翻版:

http://www.benegg.net/grade_calculator.html

if循环/变量有很多。我如何在Java中使用OOP来解决这个问题?比如使用方法/构造函数等?

4 个答案:

答案 0 :(得分:3)

你应该使用一个循环:

int sumTotal=0;
int sumWeight=0;
for(int i=0;i<counter;i++){
    double mark = input.nextDouble();
    double promark = (mark/100);
    double weight = input.nextDouble();
    sumWeight+=weight; //add to sum of weight
    sumTotal+=promark*weight; //add to sum of total
}
System.out.println("Mark is: "+(double)sumTotal/sumWeight);

未经测试,只是您应该使用的方式

答案 1 :(得分:1)

您可以创建一个类Exam作为输入数据的占位符:

public static class Exam {
  final double mark;
  final double weight;

  public Exam(double mark, double weight) {
    this.mark = mark;
    this.weight = weight;
  }

  public double getTotal() {
    return mark * weight / 100;
  }

  public static double getAverage(Exam[] arr) {
    double sum = 0;
    for (int i=0; i < arr.length; i++) sum += arr[i].getTotal();
    return sum / arr.length;
  }

}

现在您可以使用循环来创建Exam数据数组:

public static void main(String[] args) {

   Scanner input = new Scanner(System.in);
   int counter = input.nextInt();
   Exam[] arr = new Exam[counter];

   for (int i = 0 ; i < counter ; i++) {
     double mark = input.nextDouble();
     double weight = input.nextDouble();
     arr[i] = new Exam(mark, weight);
   }
   System.out.println("The average mark for your "+counter+" assignments/exams is "+ Exam.getAverage(arr));
}

希望它看起来更像是OOP代码。

答案 2 :(得分:0)

由于这是一个非常具体的应用程序,我不会为你重写它,但我会给你一些关于如何减少重复代码的一般提示。

您真正想做的是在循环中对markX,promarkX,weightX和totalX进行所有重复计算。一旦你有了,你想要将总数添加到for循环之外的变量(finalmark)。一旦循环退出,您就可以打印您的陈述。

答案 3 :(得分:0)

由于您使用数字计数器来跟踪要循环的次数,因此您应该使用for loop