开发者

I have one error with java arrays(store the letter grade in an array)

public char calculateGrade(int [] scores, char [] grades){
    for (int r = 0; r <开发者_运维知识库; scores.length; r++){
        //System.out.println(scores[r] + " ");
        if (scores[r] > 90)
            grades = 'A';
        else if (scores[r] > 80)
            grades = 'B';
        else if (scores[r] > 70)
            grades = 'C';
        else if (scores[r] > 60)
            grades = 'D';
        else
            grades = 'F';

        return grades;
    }
}

Above is one of my methods. It reads a part of a file (scores[]) and determines what letter grade they are. What I need to know how to do is store the letter grade in an array I have already crated called grades[].


grades[r] = 'A'; (and similar for others)

This assumes grades is of same length as scores


This looks suspiciously like a homework assignment, so look at how you access the scores array, and how you access the grades array. Do you see the difference?


Try this:

public void calculateGrade(int [] scores, char [] grades){
    for (int r = 0; r < scores.length; r++){
        if (scores[r] > 90)
            grades[r] = 'A';
        else if (scores[r] > 80)
            grades[r] = 'B';
        else if (scores[r] > 70)
            grades[r] = 'C';
        else if (scores[r] > 60)
            grades[r] = 'D';
        else
            grades[r] = 'F';
    }
}


grades = 'A';

A is of type char while the identifier grades is of type char[]. What you have to do is to accumulate the grades in the array using [] operator. Also you should return grades once you are done with the for loop and not at the first iteration. Make sure that the length of array grades is equivalent to scores.


public char calculateGrade(int[] scores)
{
    char grade = 'F';
    for(int score : scores)
    {
        if(score >= 90) grade = 'A';
        else if(score >= 80) grade = 'B';
        else if(score >= 70) grade = 'C';
        else if(score >= 60) grade = 'D';
        else grade = 'F';
    }
    return grade;
}


Avoid side-effects in functions, where possible, consider this as an alternative approach:

// Given a score, return the appropriate Grade for it.
public char calculateGrade(int score){
   // See function contract.
}

// Later...
int scores[] = {10,70,50,90};
int grades[] = new char[scores.length]; // but really, use an ArrayList or simialr...
for (var i = 0; i < scores.length; i++) {
   // We used this form of "for" to keep an index
   grades[i] = calculateGrade(scores[i]);
}

// And perhaps there might be a function like this as well:

// Given a set of scores, get the associated grades.
public char[] calculateGrades(int[] scores) {
   // See above.
   // Note how this avoids mutating an input parameter.
   // Keeping functions "more refined" also generally helps.
}

Happy coding.


All of what I'm seeing doesn't consider the fact that it would seem as though you are attempting to pass by reference, which cannot be done in java. You either need to change the return type of your method (currently it is expecting a single character), or have the method just access a global variable, rather than passing it a variable. It does look like homework so I'm not going to go any further.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜