-2

I'm trying to figure out the code to make a bowling scorekeeper in Java and I'm having trouble making it so that the user can input more than one strike or spare in a row. If you have any tips please let me know! thanks :) clarification edit: so the question for the code was to make a program that lets the user input the number of pins knocked down in one round of bowling with 10 frames. So far, i have the code set up so that it takes the number of pins that the user inputted and adds that to the score. I also have it set up so that the user can input only one strike or spare in a row (since those require information from the next frame). The issue that i am having is if the bowler bowls two strikes or spares in a row, or even all spares

import java.util.Scanner;
public class Bowlers {
    public static void main (String [] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("this is a bowling score keeper :)");
        System.out.println("if you roll a strike enter 0 when prompted for roll 2 :)");
        System.out.println("please dont have two fancy things in a row");
        int roll1[] = new int[11];
        int roll2[] = new int[11];
        int roll3[] = new int [1];
        int score = 0;

        for (int i = 0; i <10; i++) {
            System.out.print("enter roll 1 score: ");
            roll1[i]=s.nextInt();
            score = score+roll1[i];
            System.out.print("enter roll 2 score: ");
            roll2[i] = s.nextInt();
            score = score +roll2[i];
            if (roll1[9] == 10) {
                System.out.print("enter roll 3 score: ");
                roll3[0] = s.nextInt();
                score = score + + roll2[9] + roll3[0]; 
            }
            else if (roll1[i] + roll2[i] == 10) {
                score = score + roll1[i+1] +1;
            }
            else if (roll1[i] == 10) {
                score = score + roll1[i+1] + roll2[i+1] +1;
            }

        }

        System.out.println(score);


        //ending curlies below
    }
}
heese1421
  • 21
  • 4
  • 4
    Can you clarify what the problem is with the code you have? It's currently not clear exactly what you're asking. Please see: [ask], the site [tour], and especially [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Reinstate Monica Nov 14 '19 at 15:36
  • It seems related to this : https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Arnaud Nov 14 '19 at 15:38
  • yeah sorry, im new to this so i dont really know how to do stuff. so the question for the code was to make a program that lets the user input the number of pins knocked down in one round of bowling with 10 frames. So far, i have the code set up so that it takes the number of pins that the user inputted and adds that to the score. I also have it set up so that the user can input only one strike or spare in a row (since those require information from the next frame). The issue that i am having is if the bowler bowls two strikes or spares in a row, or even all spares. – heese1421 Nov 14 '19 at 15:40

1 Answers1

3

There doesn't look like there is anything stopping two strikes/spares from being entered in a row, it just looks like the score isn't correct when doing so, particularly when it happens in the last frame. Bowling isn't scored going forward (hence why the roll[i+1] isn't working), you need to score it going backwards, managing the state of the players game.

With regards to the question - you could have included some sample input/output and referenced what was expected vs what you were getting.

I would suggest using the language to your advantage and breaking down the game into manageable blocks. The following might help (or might not) but you should definitely try to start thinking a little greater than arrays of throws.

A game is made up things:

public class Game {
    private List<Player> players;
}

public class Player {
    private Frame[] frames;
} // Since it's more fun to bowl with multiple people

public class Frame {
    private int[numBalls];      // scores per frame
    private int total;

    public int getScore() {};      // total
    public int getBallsThrown() {} // how many balls
    public int getBallsLeft() {};  // how many left
}

Since more bowling scores show all balls rolled (not just frame total) you'd want to keep all that information. Now you accept scores into a pre-populated Frame[10] where:

new Frame(2);   // 1st - 9th
new Frame(3);   // 10th

If you are entering scores, you can now do:

9, 0, 9, 0, 10, 10, 10, ...

which will result in:

Frame[0]: 9, 0
Frame[1]: 9, 0   // Keep missing the 10 pin
Frame[2]: 10
Frame[3]: 10

It's the responsibility of your Game to manage the scores for each input score. For that reason after each ball, you'd need to determine whether a previous score needs to be re-calculated.

[9,0] [9,0] [10] [10] [ ]
[9  ] [18 ] [  ] [  ] [ ]

When the person rolls their next ball (9,), the game now has enough information to add up Frame[2]:

[9,0] [9,0] [10] [10] [9,]
[9  ] [18 ] [47] [] [ ]

This can be done by storing the state of the players game, which will either be OPEN (don't look back), SPARE (look back after 1 input), STRIKE (look back after 2 inputs) to determine whether you should bother looking back - just remember the special case of two X X in a row, you need to keep the STRIKE state but reduce the ball count for the next ball.

kendavidson
  • 1,245
  • 1
  • 8
  • 16