我无法从我的数组中返回一个值

时间:2013-11-06 16:33:09

标签: java arrays

这是经过评估的工作,所以请不要直截了当地回答。

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

公共类MarkCalculator {     static int [] marks = 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;
}

}

3 个答案:

答案 0 :(得分:3)

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

这是一个问题。加权没有初始尺寸。

答案 1 :(得分: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);

答案 2 :(得分:1)

问题是加权是空的

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]);
}