作业帮 > 综合 > 作业

谁帮我做下这道java的题呀

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/15 05:22:10
谁帮我做下这道java的题呀
创建checkAnswer方法实现不定项选择判分,具体要求如下:
1)考生答案和正确答案相同(不考虑选项顺序),
2)考生答案不全,得2分.
3)考生答案中有错误答案,得0分.
部分示例如下:
得分
ABC ABC/ACB/BAC/CBA/BCA 5
ABC A/B/C/AB/AC/BC/CB/CA/BA 2
ABC D/AD/BD/CD/ABD/BCD/ACD/ABCD 0
————————————————————————————————————
public class JavaMethod {
public static int checkAnswer(String correctAnswer,String userAnswer){
}
public static void main(String[] args) {
String correctAnswer = "ACD";
String userAnswer = "AD";
int grade = checkAnswer(correctAnswer,userAnswer);
System.out.println("本题的得分是 :" + grade);
}
}
————————————————————————————————————
谁帮我做下这道java的题呀
 public static int checkAnswer(String correctAnswer, String userAnswer)
    {
        int grade = 0;
        int num = 0;
        for (int i = 0; i < userAnswer.length(); i++)
        {
            // 如果用户答案某一项在正确答案中
            if (correctAnswer.indexOf(userAnswer.charAt(i)) != -1)
            {
                num++;
            }
            // 如果用户答案某一项不在正确答案中(即有错误选择项)
            else
            {
                return grade = 0;
            }
        }
        if (num == 0)
        {
            grade = 0;
        }
        else if (num == correctAnswer.length())
        {
            grade = 5;
        }
        else
        {
            grade = 2;
        }
        return grade;
    }
    
    public static void main(String[] args)
    {
        String correctAnswer = "ABC";
        String userAnswer = "D";
        int grade = checkAnswer(correctAnswer, userAnswer);
        System.out.println("本题得分 : " + grade);
    }