保龄球得分计算代码
示例: 全中 = x, 补中 = 9/ , 不中 = -
public static void main(String[] args) {
sum();
}
// 汇总结果
public static void sum(){
//String scan="xxxxxxxxxxxx";
//String scan="9/9/9/9/9/9/9/9/9/9/9";
String scan="x9/x81x7/9-8/72xx8";
//String scan="7-8/9/x8-xxx8/9/8";
//String scan="----------";
char [] ch=scan.toCharArray();
/* 依次为
* 统计结果
* 轮次----10轮次为一局
* */
int result=0;
int lunci=0;
for (int i = 0; i < ch.length; i++) {
// 1~9 lunci
if(lunci<9){
if(ch[i]=='x'){
result=result+xbc(ch[i],ch[i+1],ch[i+2]);
}
else if(ch[i+1]=='/'){
result=result+abc(ch[i],ch[i+1],ch[i+2]);
i++;
}
else{
result=result+ab(ch[i],ch[i+1]);
i++;
}
System.out.println("result="+result);
lunci++;
}
// 10 lunci
if(lunci==9){
i++;
if(ch[i]=='x'){
result=result+xbc(ch[i],ch[i+1],ch[i+2]);
}
else if(ch[i+1]=='/'){
result=result+abc(ch[i],ch[i+1],ch[i+2]);
}
else{
result=result+ab(ch[i],ch[i+1]);
}
System.out.println("result="+result);
break;
}
}
System.out.println("endResult="+result);
}
/*char a,char b,char c
* a=x
* x,b,c
* */
public static int xbc(char a,char b,char c){
int rs=0;
if(c=='/'){
rs=20;
}
else{
rs=10+change(b)+change(c);
}
return rs;
}
/*char a,char b,char c
* b=/
* a,b and c
* */
public static int abc(char a,char b,char c){
int rs=10+change(c);
return rs;
}
/*char a,char b
* a+b<10
* a , b
* */
public static int ab(char a,char b){
int rs=change(a)+change(b);
return rs;
}
// char---int
public static int change(char a){
if(a=='x'){
return 10;
}
else if(a=='-'){
return 0;
}
else{
int i=Integer.parseInt(a+"");
return i;
}
}