1

这是评估工作,所以请不要直接回答。

我的程序应该计算用户等级:“通过”、“失败”或“通过补偿”。但是,它不会返回答案。我无法弄清楚为什么 - 任何人都可以帮忙吗?

公共类 MarkCalculator { 静态 int[] 标记 = new int[12];

//public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int weighting;
    int coursework;
    int exammark;

    System.out.println("Please enter course work weighting");
    marks[0]= kb.nextInt();

    System.out.println("Please enter course work mark");
    marks[1]= kb.nextInt();

    System.out.println("Please enter exam mark");
    marks[2]= kb.nextInt();


    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

}




public String[] computeMarks(int[] marks) {

    final int[] WEIGHTING = {55,66,55,44,33,44};
    String[] results = new String[WEIGHTING.length];
    for (int i = 0; i < marks.length / 2; i++) {

        int exam = marks[i];
        int cw = marks[i];
        int weight = WEIGHTING[i];
        int formula = ((cw + weight) + (exam * (100 - weight)) / 100);

        if (formula >= 40){
            results[i]="PASS";
        } else if ((formula < 39) && (formula > 35)){
            results[i]="COMPENSATION PASS";
        }else{
            results[i]="FAIL";

        }

    }

    return results;
}

}

4

3 回答 3

3
final int[] WEIGHTING = {};
String[] results = new String[WEIGHTING.length];

这是一个问题。WEIGHTING 没有初始大小。

于 2013-11-06T16:36:26.423 回答
1

问题是 WEIGHTING 是空的

final int[] WEIGHTING = {}; // empty
String[] results = new String[WEIGHTING.length];
for (int i = 0; i < marks.length / 2; i++) {

    int exam = marks[i];
    int cw = marks[i];
    int weight = WEIGHTING[i]; // You cant access elements from an empty array

    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

在这里你传递的是空的分数。

编辑

您的程序无法运行的原因是您没有从 computeMarks 中获取结果。您应该将它存储在 main 内的数组中,例如

String[] result = mc.computeMarks(marks);

for(int k=0;k<result.length;k++)
{
    System.out.println(result[k]);
}
于 2013-11-06T16:39:29.283 回答
1

另外:您不会用任何东西初始化标记。尝试这个:

System.out.println("Please enter course work weighting");
        marks[0]= kb.nextInt();

        System.out.println("Please enter course work mark");
        marks[1]= kb.nextInt();

        System.out.println("Please enter exam mark");
        marks[2]= kb.nextInt();

        MarkCalculator mc = new MarkCalculator();
        mc.computeMarks(marks);
于 2013-11-06T16:38:56.190 回答