黑马程序员技术交流社区
标题:
考试打分程序
[打印本页]
作者:
何亚辉
时间:
2016-5-29 10:57
标题:
考试打分程序
package com.heima;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClassTest {
public static void main(String[] args) {
String s = "A,AB,CD,AC,AC,B,D,A,C,C,AB,ABCD,CD,BD,AD,CD,A,A,C,D";
System.out.println(getScore(s));
}
public static String getScore(String s ){
String regex = "[ABCD]{1,4}";
String[] std ={ "A","AB","CD","AC","B","D","A","C","D","B","A","C","D","A","AD","CD","A","A","C","D"};
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
String[] s1 = new String[20];
for (int i = 0;m.find() ; i++) {
s1[i]=m.group();
}
for (int i = s1.length-1; i >=0 ; i--) {
if(i==s1.length-1&&s1[i]!=null){
break;
}
else if (s1[i]!=null){
return "您少写了"+(19-i)+"个答案";
}
}
int Score = 100;
for (int i = 0; i < s1.length; i++) {
if (!(s1[i].equals(std[i]))){
Score -=5;
}
}
return "您的分数为"+Score;
}
}
作者:
一架飞机CE3
时间:
2016-5-29 16:22
蛮好的
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2