0

此代码的目标是比较两个不同的 string[] 数组,并检查有多少元素匹配。这将允许这些方法查看学生是否通过了测试,以及确定他们的字母等级。但是,完成的每项测试都会返回 true 以表示他们通过,并返回“A”表示他们的字母等级。现在元素被正确比较了,我仍然有每个元素都通过 .equals() 的问题。即 answerKey[1] = "B"; studentAnswers[1] = "C"; 如果要比较这两个元素,它将通过.equals().

public class ListOfAnswers
{
    static final String[] answerKey = {"A", "B", "B", "C", "D", "B", "C", "C", "D", "E",
                                       "C", "D", "D", "E", "A", "A", "A", "D", "D", "E"};
    String[] studentAnswers;

    public ListOfAnswers(String[] ans)
    {
        studentAnswers = ans;
    }

    public boolean checkPassOrFail()
    {
        int answersRight = 0;
        for (int cnt = 0; cnt < studentAnswers.length; cnt++)
        {
            if(studentAnswers[cnt].equals(answerKey[cnt]))
            {
                answersRight++;
            }
        }
        if (answersRight >= 14)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public String computeGrade()
    {
        int score = 0;
        String grade = "";
        for (int cnt = 0; cnt < studentAnswers.length; cnt++)
        {
            if (studentAnswers[cnt].(answerKey[cnt]))
            {
                score++;
            }
        }

        if (score < 10)
        {
            grade = "E";
        }
        else if (score == 10)
        {
            grade = "D";
        }
        else if ((score==11)||(score==12))
        {
            grade = "C-";
        }
        else if (score==13)
        {
            grade = "C";
        }
        else if (score == 14)
        {
            grade = "C+";
        }
        else if (score == 15)
        {
            grade = "B-";
        }
        else if (score == 16)
        {
            grade = "B";
        }
        else if (score == 17)
        {
            grade = "B+";
        }
        else if (score == 18)
        {
            grade = "A-";
        }
        else if ((score == 19)||(score==20))
        {
            grade = "A";
        }
        return grade;
    }
} 

测试数据如下

public class ListOfAnswersTester
{
    public static void main(String[] args)
    {
        String[] danAnswers = {"A", "B", "B", "D", "D", "B", "C", "C", "D", "E", "C",
                               "D", "D", "E", "A", "A", "A", "D", "D", "E"};
        ListOfAnswers danAnswerList = new ListOfAnswers(danAnswers);

        String[] bobAnswers = {"A", "C", "B", "C", "D", "B", "C", "C", "D", "E", "C",
                               "D", "A", "E", "A", "A", "A", "D", "E", "E"};
        ListOfAnswers bobAnswerList = new ListOfAnswers(danAnswers);

        System.out.println("Student Dan's Pass/Fail: " + danAnswerList.checkPassOrFail());
        System.out.println("Student Dan's Grade: " + danAnswerList.computeGrade());

        System.out.println("Student Bob's Pass/Fail: " + bobAnswerList.checkPassOrFail());
        System.out.println("Student Bob's Grade: " + bobAnswerList.computeGrade());
     }
}
4

3 回答 3

2

问题在这里:

if(studentAnswers[cnt] == answerKey[cnt])

要比较Strings 和其他 Object 实例,必须使用equals方法:

if(studentAnswers[cnt].equals(answerKey[cnt]))

这必须在方法ListOfAnswers#checkPassOrFailListOfAnswers#computeGrade.

于 2013-05-08T15:26:36.537 回答
1

在 Java 中,当你想比较对象时,你必须使用 equal();

studentAnswers[cnt] == answerKey[cnt]

studentAnswers[cnt].equals(answerKey[cnt])
于 2013-05-08T15:28:11.227 回答
0

我在这里看到一个错字:

ListOfAnswers bobAnswerList = new ListOfAnswers(danAnswers);

应该:

ListOfAnswers bobAnswerList = new ListOfAnswers(bobAnswers);

或者你把丹的好答案给鲍勃;)

哦,正如其他人所说,equals在比较(非空)字符串时总是使用,除非你真的想检查身份(我看到问题已经更新了)。

于 2013-05-08T15:43:26.720 回答