栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

题目如下:

我的代码:

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);

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号