【2020.01.03】算法学习记录——11计分

算法-11计分


输入一个只包含“w”与“z”的字符串,分别代表小王与小张两个人进行比赛的得分。
e.g. “w,w,w,z,z,z” 表示小王先得3分,小张后得3分。
现有如下计分规则:
一轮比赛首先获得11分者胜利,胜利后双方进入下一轮比赛。
需要输出双方每一轮的比分和当前轮的比分。
注: 输入的字符串并不代表比赛完全结束,有可能是比赛进行当中;


public class ScoreElevenAndTwentyOne
{
    public String findEleven(String input){
        char[] rawList = input.toCharArray();
        String result = "";
        int round = 1;
        int scoreA = 0;
        int scoreB = 0;
        for(int i = 0; i < rawList.length; i++){
            if(scoreA<=11 && scoreB<=11){
                if(rawList[i] == 'w'){
                    scoreA ++;
                }
                if(rawList[i] == 'z'){
                    scoreB ++;
                }
            }
            if(scoreA >11 || scoreB > 11){
                if(scoreA>11){
                    scoreA = 11;
                }
                if(scoreB>11){
                    scoreB = 11;
                }
                result += "For Round" + round + ",the score is: " + "WANG: " + scoreA + " ; ZHANG: " + scoreB + " .";
                if(rawList[i] == 'w'){
                    scoreA = 1;
                    scoreB = 0;
                }
                if(rawList[i] == 'z'){
                    scoreA = 0;
                    scoreB = 1;
                }
                round++;
            }
            if(i == rawList.length-1){
                result += "For Round" + round + ",the score is: " + "WANG: " + scoreA + " ; ZHANG: " + scoreB + " .";
            }
        }
        return result;
    }
}


发布了17 篇原创文章 · 获赞 0 · 访问量 331

猜你喜欢

转载自blog.csdn.net/cletitia/article/details/103817774