Write your own Java bowling score

Source: Internet
Author: User

package com.java.bowlingscore1;import java.util.arrays;public class game {     int[] bowlingscore = new int[21]; //used to store the number of throws knocked down      int ball=0;                        //array subscript     int score;//score      int countframe=0;//record is currently the first round of     boolean firstthrow=true;// Judging is not the first time to throw     public void add (int pins) {//Add fractions   add fractions to an array          bowlingScore[ball++] = pins;         judgefirstthrow (pins);           //call the following judgment function     }/*firstthrow  The default is true by the rule can know the first ball if the whole into the next round, so  *  first judge added is not for 10, is directly let Countframe add 1, not just to change Firstthrow to false, *  *   the second call to add when Firstthrow to false cast two round the end of  countframe plus 1, *   Change Firstthrow to True **/  public void judgefirstthrow (int pins) {      if (Firstthrow) {        if (pins==10) {             countframe++;         }else{            firstThrow=false;         }    }else{         firstThrow=true;        countframe++;     }}public int getcurrentframe () { //returns this is the first round of     return countframe;} Public int score () {       //Returns the current round's score      Return scoreforframe (countframe);} /* *  Current wheel calculation method cycles The current number of rounds  * a[0] assignment the first stroke firstthrow  then the subscript self-increment one; Judging the first time, it's not a knockout. 10  *  Rules   Bowling round two balls   first full middle is the previous score +10+ two throws in the back of the score  *  fill   two times total knockdown 10   score plus two knockdown 10 points plus back score   * */public int scoreforframe (Int frame) {    int score=0;     int ball=0;    for (int countframe=0;countframe<frame; countframe++) {        int firstthrow = bowlingscore[ Ball++];        if (firstthrow==10) {             score= score+10+bowlingScore[ball]+bowlingScore [ball+1];         }else{             int secondThrow = bowlingScore[ball++];             int framscore = firstthrow+secondthrow;             if (framscore==10) {                 score=framScore+bowlingScore[ball]+score;             }else{                 score = framScore+score;             }        }    }     return score;} Test Public static void main (String[] args)  {game g = new game (); G.add (1); G.add (4); G.add (4); G.add (5); G.add (6); G.add (4); G.add (5); G.add (5); G.add (0); G.add (1); G.add (7); G.add (); G.add (3); G.add (6); G.add (4); G.add (8); G.add (2); G.add (); G.add (6); System.out.println (Arrays.tostring (g.bOwlingscore)); System.out.println (G.getcurrentframe ()); for (int i = 1;i<11;i++) {System.out.print ( G.scoreforframe (i) +   "  ");}}

Oneself did not read a book to knock, is the first read books to get rid of the influence of the idea of the book, the next time or should first knock over in a book. Do the test is also the function of writing a main function to do the test, it is not that what agile development like writing test class, refactoring is a little bit of the feeling of refactoring

Write your own Java bowling score

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.