日期:2014-05-20  浏览次数:20924 次

昨天转正,做了道恶心的题,保龄球计分的,大家给点意见
编程题目如下:
第一题:
打保龄球是用一个滚球去打出十个站立的柱,将柱击倒。一局分十轮,每轮可滚球一次或多次,以击倒的柱数为依据计分。一局得分为十轮得分之和,而每轮的得分不仅与本轮滚球情况有关,还可能与后续一两轮的;滚球情况有关。即,某轮某次滚球击倒的柱数不仅要计入本轮得分,还可能会计入前一两轮得分。具体的滚球击柱规则和计分方法如下:
(1) 若某一轮的第一次滚球就击倒全部十个柱,则本轮不再滚球。(若是第十轮则还需另加两次滚球)。该轮得分为本次倒柱数 10 与以后两次滚球所击倒柱数之和。
(2) 若某一轮的第一次滚球未击倒十个柱,则可对剩下未倒的柱再滚球一次。如果这两次滚球击倒全部十个柱,则本轮不再滚球(若是第十轮则还需另加一次滚球),该轮得分为本次倒柱数10与以后一次滚球所击倒柱数之和。
(3) 若某一轮的两次滚球未击倒全部十个柱,则本轮不再继续滚球,该轮得分为这两次滚球击倒的柱数这和。
总之,若一轮中一次滚球或两次滚球击倒十个柱,则本轮得分是本轮首次滚球开始的连续三次滚球击倒柱数之和(其中有一次或两次不是本轮滚球)。若一轮内二次滚球击倒柱数不足十个,则本轮得分即为这两次击倒柱数之和。
以实例说明如下:
轮 1 2 3 4 5 6 7 8 9 10
各轮第一次得分10 10 10 7 9 8 8 10 9 10 8 #p#
各轮第二次得分/ / / 2 1 1 2 / 1 / 2-
各 轮 得 分30 27 19 9 18 9 20 20 20 20
累 计 总 分30,57,76,85,103,112,132,152,172,192
要求:
1.使用你熟悉的程序语言(C#,java,javascript 之一)写一个程序可以方便的对保龄球运动项目进行记分;
界面布局合理,能直观的显示每次得分,每轮得分和累计得分的情况
2.分值录入操作合理,需要有必要的无效值判断处理(比如每次击球得分不会>10等等)
3.每场比赛结束后将本次比赛数据保存下来。(选择文件或者数据库均可),

我昨天是用JAVA直接在控制台输出的,每次由我来输入击倒的数量,然后得分。我的代码算法太乱了,就不贴出来见笑了,大家还有写什么别的好办法吗?网上COPY的代码就不要来了,给点好的思路什么的,关于第2点验证和将最终数据保存成文件怎么弄啊~!希望大家一起交流下

------解决方案--------------------

------解决方案--------------------
这个一步步做下来没什么难的吧。。。
------解决方案--------------------
这个应该毫无压力才对
------解决方案--------------------
这个项目起码30万,有本事就搞定,没本事你就让别人搞定。
------解决方案--------------------
看得出 你公司招人就招会这个题目的人了 因为当前公司肯定没人会

要是会肯定不会让你来做


------解决方案--------------------
这打保龄球的界面该怎么弄啊..
------解决方案--------------------
探讨
老板说这个就是以前公司的面试题,现在拿来转正做。没做好走人............

------解决方案--------------------
探讨

这个项目起码30万,有本事就搞定,没本事你就让别人搞定。

------解决方案--------------------
mark
------解决方案--------------------
文字太多了,看不下去
------解决方案--------------------
我记得这题哪本书有来着??好像是极限编程的结对???
------解决方案--------------------
Java code

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Bowling {

    public static ArrayList<Round> rounds = new ArrayList<Round>();

    public static void main(String[] args) {
    // init();//init() 为了调试
    input();
    countScore();
    countTatal();
    display();
    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    public static void init() {
    // 读取数据过程 每次都大于0小于10;
    rounds.add(new Round(10, 0));
    rounds.add(new Round(10, 0));
    rounds.add(new Round(10, 0));
    rounds.add(new Round(7, 2));
    rounds.add(new Round(9, 1));
    rounds.add(new Round(8, 1));
    rounds.add(new Round(8, 2));
    rounds.add(new Round(10, 0));
    rounds.add(new Round(9, 1));
    rounds.add(new Round(10, 8));
    if (rounds.get(9).firstNumber == 10)
        rounds.add(new Round(2, 0));
    // 为了简化计算,添加虚拟轮数,实际上不存在;;

    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    public static void input() {// 控制台输入,模拟击柱数
    Scanner scanner = new Scanner(System.in);
    String regex = "\\d|10";
    String temp = null;
    int firstNumber = 0, secondNumber = 0;

    for (int i = 0; i < 10; i++, firstNumber = 0, secondNumber = 0) {
        temp = "";
        System.out.println("第" + (i + 1) + "轮第一次击柱数");
        while (!(temp = scanner.nextLine()).matches(regex))
        ;
        ;
        firstNumber = Integer.parseInt(temp);
        if (firstNumber != 10) {
        System.out.println("第" + (i + 1) + "轮第二次击柱数");
        while (!(temp = scanner.nextLine()).matches(regex))
            ;
        ;
        secondNumber = Integer.parseInt(temp);
        }
        rounds.add(new Round(firstNumber, secondNumber));
    }
    Round round = rounds.get(9);// 第9轮
    System.out.println("倒数第二次击柱数");
    while (!(temp = scanner.nextLine()).matches(regex))
        ;
    ;
    round.secondNumber = Integer.parseInt(temp);
    if (round.firstNumber == 10
        || round.firstNumber + round.secondNumber == 10) {
        System.out.println("最后一次击柱数");
        while (!(temp = scanner.nextLine()).matches(regex))
        ;
        ;
        firstNumber = Integer.parseInt(temp);
        rounds.add(new Round(firstNumber, 0));
    }

    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    public static void display() {//输出到控制台和文件
    StringBuffer sb = new StringBuffer();
    sb.append("轮数:        ");
    for (int i = 1; i <= 10; i++) {
        sb.append(String.format("%5d", i));
    }
    sb.append("\r\n第一次击球得分:");
    for (int i = 0; i < 10; i++) {//
        sb.append(String.format("%5d", rounds.get(i).firstNumber));
    }
    sb.append("\r\n第二次击球得分:");
    for (int i = 0; i < 10; i++) {
        Round round = rounds.get(i);
        if (round.firstNumber == 10)
        sb.append(String.format("%5d", rounds.get(i).secondNumber));
        else
        sb.append("    /");
    }
    sb.append("\r\n本轮得分:     ");
    for (int i = 0; i < 10; i++) {
        sb.append(String.format("%5d", rounds.get(i).score));
    }
    sb.append("\r\n累计得分:     ");
    for (int i = 0; i < 10; i++) {
        sb.append(String.format("%5d", rounds.get(i).total));
    }
    sb.append("\r\n");
    System.out.println(sb.toString());
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(
            "c:/Bowling.txt"));
        bw.write(sb.toString());
        bw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

    // ///////////////////////////////////////////////////////////////////////////////////////////

    public static void countScore() {// 统计各轮分数
    Round round = rounds.get(9);// 第9轮
    if (round.firstNumber == 10
        || round.firstNumber + round.secondNumber == 10)
        round.score = round.firstNumber + round.secondNumber
            + rounds.get(10).firstNumber;
    else
        round.score = round.firstNumber + round.secondNumber;
    // ---------------------------------------------
    for (int i = 8; i >= 0; i--) {// 第8轮,第7轮...第0轮
        round = rounds.get(i);
        Round tempRound = rounds.get(i + 1);
        if (round.firstNumber == 10) {// 首次分数为10
        if (tempRound.firstNumber == 10)
            round.score = round.firstNumber + tempRound.firstNumber
                + rounds.get(i + 2).firstNumber;
        else
            round.score = round.firstNumber + tempRound.firstNumber
                + tempRound.secondNumber;
        } else if (round.firstNumber + round.secondNumber == 10)// 前两次分数为10
        round.score = round.firstNumber + round.secondNumber
            + tempRound.firstNumber;
        else
        round.score = round.firstNumber + round.secondNumber;// 首次分数为10
    }
    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    public static void countTatal() {// 统计前n轮的累加分数
    rounds.get(0).total = rounds.get(0).score;// 第0轮
    for (int i = 1; i < 10; i++)
        // 第1轮 第2轮...第9轮
        rounds.get(i).total = rounds.get(i).score + rounds.get(i - 1).total;
    }

}

class Round {
    private static int counter = 0;// 计数器
    private final int id = counter++;// id 代表 第id轮,id = 0,1,2,3,4,5,6,7,8,9
                    // (可能有10)
    int firstNumber;// 本轮击倒第一次击倒柱数
    int secondNumber;// 本轮击倒第二次击倒柱数
    int score;// 本轮得分
    int total;// 至本轮累计总分

    public Round(int number, int secondNumber) {
    this.firstNumber = number;
    this.secondNumber = secondNumber;
    }
}
/*
第1轮第一次击柱数
10
第2轮第一次击柱数
10
第3轮第一次击柱数
10
第4轮第一次击柱数
7
第4轮第二次击柱数
2
第5轮第一次击柱数
9
第5轮第二次击柱数
1
第6轮第一次击柱数
8
第6轮第二次击柱数
1
第7轮第一次击柱数
8
第7轮第二次击柱数
2
第8轮第一次击柱数
10
第9轮第一次击柱数
9
第9轮第二次击柱数
1
第10轮第一次击柱数
10
倒数第二次击柱数
8
最后一次击柱数
2
轮数:            1    2    3    4    5    6    7    8    9   10
第一次击球得分:   10   10   10    7    9    8    8   10    9   10
第二次击球得分:    0    0    0    /    /    /    /    0    /    8
本轮得分:        30   27   19    9   18    9   20   20   20   20
累计得分:        30   57   76   85  103  112  132  152  172  192

*/