题目不难-Java解决《保龄球》问题

题目不难-Java解决《保龄球》问题,第1张

前几天刷题,刷到一个有趣的题目,因为没有打过保龄球,就连题目都要读好久!

大概是:每一轮都两个分数,通过给定一个选手所有的分数计算总分;

计分规则大家可以百度一下。

题目如下:

我的代码:

public class Bowling {
//    方法-输入投球情况
    public static char[] setBowl(){
        System.out.println("请输入您的投球情况:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] bowls = str.toCharArray();
        return bowls;
    }

//    计算每轮的分数
    public static Integer getScore(char[] bowls){
        int score = 0;
        int sign = 0;
        int i = 0,j = 0;
        while(j < 10) {
            if(i+1>=bowls.length){
                break;
            }
            if (bowls[i] == 'X') {
                score += 10;
                if (bowls[i + 1] == 'X') {
                    score += 10;
                } else {
                    score += Integer.parseInt(bowls[i + 1] + "");
                }
                if (bowls[i + 2] == 'X') {
                    score += 10;
                } else if (bowls[i + 2] == '/') {
                    score += 10 - Integer.parseInt(bowls[i + 1] + "");
                } else {
                    score += Integer.parseInt(bowls[i + 2] + "");
                }
                ++i;
                ++j;
            } else if (bowls[i] == '/') {
                score += 10 - Integer.parseInt(bowls[i - 1] + "");
                if (bowls[i + 1] == 'X') {
                    score += 10;
                } else {
                    score += Integer.parseInt(bowls[i + 1] + "");
                }
                ++i;
                ++j;
                sign = 0;
            } else if (bowls[i] == '-') {
                score += 0;
                ++sign;
                ++i;
                if (sign == 2) {
                    sign = 0;
                    ++j;
                }
            } else {
                score += Integer.parseInt(bowls[i] + "");
                ++i;
                if (sign == 2) {
                    sign = 0;
                    ++j;
                }
            }
        }
        return score;
    }

//    X72X9/7-9/XX9/9-
//    X7/368-XX8/637/9/X
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Bowling b = new Bowling();
        char[] bowls = b.setBowl();
        int sumScore = b.getScore(bowls);
        System.out.println("您获得的分数为:"+sumScore);

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/741852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存