大家看一下这个程序怎么改正

问题如下:
(1)在一周的7天内,如果学生连续两天以上未进行核酸PCR检测,或者如果在7天内检测次数少于或等于3次,他/她将得到相应的注意。“Y”表示该测试完成,“N”表示测试尚未完成。我们使用长度为7的字符串说明学生在某一周内参与核酸PCR检测的情况。使用系统时,用户需要输入学生ID和参与状态(7位字符串)。当输入满足上述要求时系统将输出:“满足”,学生ID必须为八位整数且首位不为0,且参与状态不能出现其他字母,否则输出:“不符合要求”
(2)在第一问前提下,假定COVID-19仅持续4周,每个学生的初始分数是100分。每周都会对学生进行评估,看他们是否根据其核酸PCR检测参与情况看他们是否违反纪律。第一次违反规定,扣10分。第二次15分、第三次20分和第四次25分。学生必需的一次性输入4周的核酸参与状态(每周用空格隔开),然后输出他/她的分数。注意:周与周之间只能用一个空格隔开,否则输出不符合规定

我的代码:

import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String id = input.next();
        if (id.length()>8){
            System.out.print("Not Valid");
        }else {
            int ID = Integer.parseInt(id);
            boolean b = (ID<10000000 || ID>99999999);
            boolean a = ID>=10000000 && ID<=99999999;
            String s = input.next();
            Pattern pattern = Pattern.compile("^[YN]{7}$");
            boolean matches = pattern.matcher(s).matches();
            if (!matches || b){
                System.out.print("Not Valid");
            }
            String[] split = s.split("");
            int sum=0;
            boolean flag = false;
            for (int i = 0; i<split.length-1; i++){
                if (split[i].equals("Y")){
                    sum++;
                    if (split[i].equals(split[i+1])){
                        flag=true;
                    }
                }
            }
            if (matches && a && sum>3 && flag){
                System.out.print("Good, keep it up!");
            }
            if (matches && a && sum<=3 || !flag){
                System.out.print("Has not participated in Nucleic Acid PCR tests as required!");
            }
        }
    }
}

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int score = 100;
        int d = 10;
        Pattern pattern = Pattern.compile("^[YN]{7}[ ][YN]{7}[ ][YN]{7}[ ][YN]{7}$");
        String x = input.nextLine();
        Matcher matcher = pattern.matcher(x);
        if (matcher.matches()) {
            String[] array = x.split(" ");
            for (int i = 0; i < 4; i++) {
                if (array[i].contains("YYY") && array[i].replaceAll("N", "").length() > 3)
                {
                    score = 100;
                } else {
                    score -= d;
                    d += 5;
                }
            }
            System.out.printf("%d",score);
        } else {
            System.out.print("Not valid");
        }
    }
}

我自己写的两个都不太对,oj上成功率只有一半左右,希望大家能帮我改一改,感谢

第1小问的答案修改如下:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String id = input.next();
        if (id.length()>8){
            System.out.print("Not Valid");
        }else {
            int ID = Integer.parseInt(id);
            boolean b = (ID<10000000 || ID>99999999);
            boolean a = ID>=10000000 && ID<=99999999;
            String s = input.next();
            Pattern pattern = Pattern.compile("^[YN]{7}$");
            boolean matches = pattern.matcher(s).matches();
            if (!matches || b){
                System.out.print("Not Valid");
            }
            String[] split = s.split("");
            int sum=0;
            // flag代表连续的N
            boolean flag = false;
            // 修改1:循环7次
            for (int i = 0; i<split.length; i++){
                if (split[i].equals("Y")){
                    sum++;
                    // 修改2:这里不应该记录连续Y,应该要记录连续N
//                    if (split[i].equals(split[i+1])){
//                        flag=true;
//                    }
                } else {
                    if (i<6 && split[i].equals(split[i+1])){
                        flag=true;
                        break;
                    }
                }
            }
            // 修改3:flag判断条件也相应反过来
            if (matches && a && sum>3 && !flag){
                System.out.print("Good, keep it up!");
            } else if (matches && a && (sum<=3 || flag)){
                System.out.print("Has not participated in Nucleic Acid PCR tests as required!");
            }
        }
    }

第2小问的答案修改如下:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int score = 100;
        int d = 10;
        Pattern pattern = Pattern.compile("^[YN]{7}[ ][YN]{7}[ ][YN]{7}[ ][YN]{7}$");
        String x = input.nextLine();
        Matcher matcher = pattern.matcher(x);
        if (matcher.matches()) {
            String[] array = x.split(" ");
            for (int i = 0; i < 4; i++) {
                // 修改1: 只需要判断连续的N以及Y的次数<=3就扣分,其它的情况不应该把score重置为100
                if(array[i].contains("NN") || array[i].replaceAll("N", "").length() <= 3) {
                    score -= d;
                    d += 5;
                }
//                if (array[i].contains("YYY") && array[i].replaceAll("N", "").length() > 3)
//                {
//                    score = 100;
//                } else {
//                    score -= d;
//                    d += 5;
//                }
            }
            System.out.printf("%d",score);
        } else {
            System.out.print("Not valid");
        }
    }

请采纳,十分感谢!


package pack1;

import java.util.Scanner;

public class CheckCOVID19 {
    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in);) {
            System.out.print("Enter an id(integer with 8 bits): ");
            String id = input.next();

            System.out.print("Enter status(7 bits): ");
            String status = input.next();

            System.out.println((isValidId(id) && isValidStatus(status)) ?
                    "满足" : "不符合要求");
        }
    }

    /**返回id是否有效?*/
    public static boolean isValidId(String id) {
        try {
            Integer.parseInt(id);
            return id.charAt(0) != '0' && id.length() == 8;
        }catch (Exception ex) {
            return false;
        }
    }

    /**返回参与状态是否有效?*/
    public static boolean isValidStatus(String status) {
        if (status.length() != 7) return false; //长度不为7时返回false

        int countY = 0; //计数Y
        for (int i = 0; i < status.length(); i++)
            if (status.charAt(i) != 'Y' && status.charAt(i) != 'N')
                return false;   //如果字符既不是Y也不是N,返回false
            else if (status.charAt(i) == 'Y') countY++; //如果字符为Y,countY++;

        for (int i = 0; i < status.length() - 2; i++)
            if (status.charAt(i) == 'N' && status.charAt(i + 1) == 'N' && status.charAt(i + 2) == 'N')
                return false;   //存在三天为N时,返回false
        return countY > 3; //如果countY > 3(Y的天数大于3),返回true
    }
}

img

img

img

img

img

img

img

img

package pack1;

import java.util.Scanner;

public class CheckCOVID {
    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in);) {
            System.out.println("Enter status in 4 weeks, splits by space: ");
            String weeks = input.nextLine();    //获取控制台输入

            int status = getStatus(weeks);  //返回状态并赋值给status
            System.out.println(getString(status) + (status == 1 ? getScore(100, getWeek(weeks)) : ""));
        }
    }

    /**返回状态的字符串描述*/
    public static String getString(int status) {
        switch (status) {
            case 1: return "该学生的最终分数是 ";
            case -1: return "只能用一个空格隔开周";
            case -2: return "输入的周数有误(4周)";
            case -3: return "输入的周的长度有误";
            case -4: return "周的参与状态不能有Y和N外的其他字符";
            default: return "";
        }
    }

    /**返回状态*/
    public static int getStatus(String weeks) {
        String[] week = getWeek(weeks); //获取周数组并赋值给数组引用变量

        if (!isValidCharacter(week)) return -4;

        for (String s : week)   //遍历周数组
            if (s.length() == 0) return -1; //存在多于一个空格时,返回-1
            else if (s.length() != 7) return -3;    //某个周的长度不为7时,返回-3
        return week.length == 4 ? 1 : -2;   //如果周数组的长度等于4,返回1,否则返回-2
    }

    /**返回周数组*/
    public static String[] getWeek(String weeks) {
        return weeks.split(" ");    //调用split()方法分隔空格并返回字符串数组
    }

    /**返回最终分数*/
    public static int getScore(int initiateScore, String[] weeks) {
        int lastScore = initiateScore;  //上个分数 = 初始分数

        for (int i = 0, j = 10; i < weeks.length; i++) {    //遍历周数组
            initiateScore -= isValidStatus(weeks[i]) ? 0 : j;   //某个周违反纪律时,初始分数 = 初始分数 - j
            if (initiateScore != lastScore) {   //如果初始分数不等于上个分数,
                lastScore = initiateScore;  //上个分数等于减过的初始分数
                j += 5; //j = j + 5(每次加5分)
            }
        }
        return initiateScore;
    }

    /**返回参与状态是否有效?*/
    public static boolean isValidStatus(String status) {
        if (status.length() != 7) return false; //长度不为7时返回false

        int countY = 0; //计数Y
        for (int i = 0; i < status.length(); i++)
            if (status.charAt(i) == 'Y')
                countY++;   //如果字符为N,countN++;

        for (int i = 0; i < status.length() - 2; i++)
            if (status.charAt(i) == 'N' && status.charAt(i + 1) == 'N' && status.charAt(i + 2) == 'N')
                return false;   //存在三天为N时,返回false
        return countY > 3; //如果countY > 3(Y的天数大于3),返回true
    }

    /**返回字符是否合法?*/
    public static boolean isValidCharacter(String[] weeks) {
        for (String week : weeks)
            for (char c : week.toCharArray())
                //如果字符既不是Y也不是N,返回false
                if (c != 'Y' && c != 'N') return false;
        return true;
    }
}

img

img

img

img

img

img

img

img

img

我也不会