第二种方法不会运行

时间:2013-11-07 22:18:13

标签: java methods

我创建的第二个方法,computeResult似乎不会运行。我有的规范,声明“一个方法computeResult,一个学生考试和课程标记数组,返回该学生的阶段结果(如果你愿意,你可以再次传递2个数组而不是一个)。你可以假设每个模块都是加权的同样适用于第1和第2阶段的模块。“

我以为我在我的computeResult方法中已经完成了这个,但事实并非如此。如果有人能引导我走向正确的方向,因为这是评估工作那会很酷。为明天所以需要尽快完成!

 import java.util.Scanner;

public class MarkCalculator {
//Creating an array using 12 integers for the coursework mark and exam mark
//static int[] marks = new int[12];
static int[] examMarks = {10, 20, 30, 37, 50, 60};
static int[] cwMarks = {10, 20, 30, 37, 50, 60};
// do not use: moduleMarks = {40, 55, 50};
static int[] weights = {50, 50, 50, 50, 50, 50};
public static int[] results = new int [weights.length];
//Creating an array using 6 integers for the weighting of the coursework 
//static int[] weighing = new int[6];


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

    Scanner kb = new Scanner(System.in);
    //here i'm defining my integers as weighting, coursework and exammark. 
    int weighting;
    int coursework;
    int exammark;

    /*
    for (int i = 0; i < weights.length / 2; i++) {
        System.out.println("Please enter course work weighting");
        weighting = kb.nextInt();
        weights[i] = weighting;
    }

    for (int i = 0; i < cwMarks.length / 2; i++) {
        System.out.println("Please enter course work mark ");
        coursework = kb.nextInt();
        cwMarks[i] = coursework;
    }

    for (int i = 0; i < examMarks.length / 2; i++) {
        System.out.println("Please enter exam mark ");
        exammark = kb.nextInt();
        examMarks[i] = exammark;
    }
    */
    System.out.println("Calculating Marks");
    MarkCalculator mc = new MarkCalculator();
    String[] results = mc.computeMarks(examMarks,cwMarks, weights);
    computeResult(cwMarks, examMarks);
    for (String result : results) {

        System.out.println("Results are " + result);
    }

}

public String[] computeMarks(int[] examMarks, int[] cwMarks, int[] weights) {
    int[] formula = new int[12];
    String[] results = new String[weights.length];

    for (int i = 0; i < weights.length; i++) {
        int exam = examMarks[i];
        int cw = cwMarks[i];
        int weight = weights[i];

        System.out.println("Exam mark = " + exam);
        System.out.println("Course work= " + cw);
        System.out.println("weighting=" + weight);

        formula [i]= ((cw * weight) + (exam * (100 - weight))) / 100;
         System.out.println("formula[" + i + "] = "+ formula[i]);
        if ((formula[i]<=39) && (formula[i] > 34)) {
            results[i] = "COMPENSATION PASS";}

        else if (formula[i] >= 40) {
            results[i] = "PASS";
        } 
        else {
            results[i] = "FAIL";
        }
        System.out.println();
    }
    return results;
}

public static String computeResult (int[] cwMarks2, int[] examMarks2)
{
    int sum = 0;

    for (int i = 0; i < weights.length; i++) {

    sum = sum + results[weights.length];
    }


    if ((sum<=39) && (sum > 34)) {
        System.out.println("STAGE 1: COMPENSATED PASS");

    }else if (sum >= 40) {
        System.out.println("STAGE 1: PASS");
    } 
    else {
        System.out.println("STAGE 1: FAIL");
    }
    System.out.println();



    return "";




}

}

1 个答案:

答案 0 :(得分:1)

运行代码时,您收到的是ArrayOutOfBoundsException:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at MarkCalculator.computeResult(MarkCalculator.java:89)
    at MarkCalculator.main(MarkCalculator.java:46)

这是因为你的for循环

for (int i = 0; i < weights.length; i++) {
    sum = sum + results[weights.length];
}

更改为

for (int i = 0; i < weights.length; i++) {
    sum = sum + results[i];
}

使用for循环,您希望使用该i变量迭代循环。在上面,你只是添加结果总和[6],它不存在(权重的长度为6)。

请注意,如果您在像Netbeans / Eclipse这样的IDE中运行它,或者甚至通过命令提示符运行它,您仍会看到Exception消息。使用这些消息在代码中找到问题。第一行告诉你什么是错的,下一行告诉你它发生了什么。在这种情况下,它发生在你班上的第89行。

作为补充说明,您应该查看您的方法签名:

public static String computeResult (int[] cwMarks2, int[] examMarks2)

此方法不需要返回String,也不会对返回的空String执行任何操作。将其更改为:

public static void computeResult (int[] cwMarks2, int[] examMarks2)