/**
* 计算一局得分、当前轮次
*
* @author Administrator
*
*/
public class Bowling {
int index = 0;
static int[] score = new int[24];
static {
for (int i = 0; i < score.length; i++) {
score[i] = -2;
}
}
// 保存分数
public int[] saveScore(int goalScore) {
score[index] = goalScore;
++index;
if (10 == goalScore) {
score[index] = -1;
++index;
}
return score;
}
// 计算当前轮次
public int roundTime() {
int round = 0;
for (int i = 0; i < score.length; i++) {
if (0 == i % 2 && -2 == score[i]) {
round = i / 2;
break;
} else if (0 != i % 2 && -2 == score[i]) {
round = i / 2 + 1;
break;
}
}
round = round <= 10 ? round : 10;
System.out.println("当前一共进行" + round + "轮");
return round;
}
// 计算当前的总分
public int getRoundScore() {
int roundScore = 0;
int rounds = roundTime();
for (int i = 1; i <= rounds; i++) {
roundScore += getEachRoundScore(i);
}
System.out.println("总分是" + roundScore);
return roundScore;
}
// 计算每轮得分
public int getEachRoundScore(int round) {
int eachRoundScore = 0;
int roundIndex = 2 * (round - 1);
if (10 == score[roundIndex]) {
eachRoundScore = score[roundIndex]
+ (-2 != score[roundIndex += 2] ? score[roundIndex] : 0);
if (10 == score[roundIndex]) {
eachRoundScore += (-2 != score[roundIndex += 2] ? score[roundIndex]
: 0);
} else {
eachRoundScore += (-2 != score[++roundIndex] ? score[roundIndex]
: 0);
}
} else {
eachRoundScore = score[roundIndex]
+ (-2 != score[++roundIndex] ? score[roundIndex] : 0);
if (10 == eachRoundScore) {
eachRoundScore += (-2 != score[++roundIndex] ? score[roundIndex]
: 0);
}
}
System.out.println("第" + round + "轮得分是" + eachRoundScore);
return eachRoundScore;
}
} 最近在捯饬保龄球计分代码,实现好复杂的说,光是规则就理解了半天。