Java 递归计算保龄球得分
只是为了练习,我试图用java递归地解决保龄球问题,我已经解决了这个问题,但是在非递归的解决方案中,我觉得得分部分非常适合递归。下面是我的试验,我确信它不接近正确,但这是我如何思考这个问题的Java 递归计算保龄球得分,java,recursion,Java,Recursion,只是为了练习,我试图用java递归地解决保龄球问题,我已经解决了这个问题,但是在非递归的解决方案中,我觉得得分部分非常适合递归。下面是我的试验,我确信它不接近正确,但这是我如何思考这个问题的 import java.util.Scanner; public class Bowling { int [] game; Scanner s ; public Bowling() { s= new Scanner (System.in); game = new int[21];
import java.util.Scanner;
public class Bowling
{
int [] game;
Scanner s ;
public Bowling()
{
s= new Scanner (System.in);
game = new int[21];
}
public void playGame()
{
int pins = 0;
String input ="";
boolean strickOrSpare =false;
for (int i = 0; i < 10 ; i++)
{
strickOrSpare =false;
System.out.println("You are in frame "+(i+1)+ "in the first throw, please enter the number of pins");
input = s.nextLine();
pins = Integer.parseInt(input);
game[i*2] = pins;
while( (pins > 10) || (pins <0))
{
System.out.println("You entered invalid number, please enter the number of pins for first throw");
input = s.nextLine();
pins = Integer.parseInt(input);
game[i*2] = pins;
}
if( pins == 10)
{
System.out.println("A Strik!");
strickOrSpare =true;
if (i != 9)
continue;
}
//////////////////////////////////////////////second throw////////////////////////////////////////////////////////////////////
System.out.println("Please enter the number of pins for second throw");
input = s.nextLine();
pins = Integer.parseInt(input);
game[(i*2)+1] = pins;
while(( (game[i*2] + game[(i*2)+1] > 10) || (game[i*2]+ game[(i*2)+1] < 0)) && i!=9)
{
System.out.println("You entered invalid number, please enter the number of pins for second throw");
input = s.nextLine();
pins = Integer.parseInt(input);
game[(i*2)+1] = pins;
}
if( game[i*2] + game[(i*2)+1] == 10)
{
strickOrSpare =true;
System.out.println("A Spare!");
}
////////////////////////////////////////////////Last Frame case//////////////////////////////////////////////////////
if(i == 9)
{
if( strickOrSpare )
{
System.out.println("Please enter the number of pins for third throw");
input = s.nextLine();
pins = Integer.parseInt(input);
game[(i*2)+2] = pins;
}
}
}
}
public int calculateScore()
{
return helperCalculateScore(0, 0);
}
private int helperCalculateScore(int index, int scoreSoFar)
{
if( index == 18)
{
if ( game[index] ==10)
scoreSoFar = scoreSoFar+ 10 + game[19] + game[20];
else if ( game[index] + game[index+1 ]==10)
scoreSoFar = scoreSoFar+ 10 + game[20];
else
scoreSoFar = scoreSoFar+ game[18] + game[19];
}
else if( (index%2 == 0) && (game[index]==10)) //strik
{
scoreSoFar = scoreSoFar+ 10 + helperCalculateScore (index+1, scoreSoFar) + helperCalculateScore (index+2, scoreSoFar);
}
else if( (index%2 == 1) && (game[index] + game[index-1] ==10)) //spare
{
scoreSoFar = scoreSoFar+ 10 + helperCalculateScore (index+1, scoreSoFar);
}
else
{
scoreSoFar = scoreSoFar+ game[index];
helperCalculateScore (index+1, scoreSoFar);
}
return scoreSoFar;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Bowling b = new Bowling();
b.playGame();
System.out.println(b.calculateScore());
}
}
import java.util.Scanner;
公营保龄球
{
int[]游戏;
扫描器;
公众保龄球()
{
s=新扫描仪(系统英寸);
游戏=新整数[21];
}
公共游戏
{
int引脚=0;
字符串输入=”;
布尔strickOrSpare=false;
对于(int i=0;i<10;i++)
{
strickOrSpare=false;
System.out.println(“您在第一次抛出时处于帧“+(i+1)+”,请输入管脚数”);
输入=s.nextLine();
pins=Integer.parseInt(输入);
游戏[i*2]=别针;
而((针>10)| |(针10)| |(游戏[i*2]+游戏[i*2)+1]<0))&&i!=9)
{
System.out.println(“您输入的编号无效,请输入第二次抛出的管脚编号”);
输入=s.nextLine();
pins=Integer.parseInt(输入);
游戏[(i*2)+1]=别针;
}
如果(博弈[i*2]+博弈[(i*2)+1]==10)
{
strickOrSpare=true;
System.out.println(“备用的!”);
}
////////////////////////////////////////////////最后一帧案例//////////////////////////////////////////////////////
如果(i==9)
{
如果(strickOrSpare)
{
System.out.println(“请输入第三次掷针的针数”);
输入=s.nextLine();
pins=Integer.parseInt(输入);
游戏[(i*2)+2]=别针;
}
}
}
}
public int calculateScore()
{
返回helperCalculateScore(0,0);
}
私有int-helperCalculateScore(int-index,int-scoreSoFar)
{
如果(指数=18)
{
如果(游戏[索引]==10)
记分制=记分制+10+游戏[19]+游戏[20];
否则如果(游戏[索引]+游戏[索引+1]==10)
scoreSoFar=scoreSoFar+10+游戏[20];
其他的
scoreSoFar=scoreSoFar+游戏[18]+游戏[19];
}
如果((索引%2==0)和(&(游戏[索引]==10))//罢工
{
scoreSoFar=scoreSoFar+10+helperCalculateScore(索引+1,scoreSoFar)+helperCalculateScore(索引+2,scoreSoFar);
}
如果((索引%2==1)和(&&(游戏[索引]+游戏[索引-1]==10))//备用
{
scoreSoFar=scoreSoFar+10+helperCalculateScore(索引+1,scoreSoFar);
}
其他的
{
scoreSoFar=scoreSoFar+游戏[索引];
helperCalculateScore(索引+1,评分标准);
}
回程记分;
}
公共静态void main(字符串[]args)
{
//TODO自动生成的方法存根
保龄球b=新保龄球();
b、 游戏();
System.out.println(b.calculateScore());
}
}
不要在计算罢工或备件的块中使用递归,只需分别添加下2帧或1帧的值即可。之后,您可以分别对index+2
或index+1
进行递归调用。确保在任何时候进行递归调用时,都会根据它返回的值递增分数。你忘了在你的else
模块中。在我的一次工作面试中,我被要求实现一个ScoringBowling
计算器。您可以在GitHub上找到我的解决方案:
我知道现在为时已晚,但可能对其他人有帮助:)你的问题是什么?这个问题的最佳解决方案是在游戏进行时增加分数。你必须确保在一次打击或空袭后,在接下来的2/1帧中得分加倍。第10帧的逻辑性更强一点。