Advertisement
fosterbl

Pool.java

Oct 4th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Pool{
  4.    public static void main(String[] args){
  5.       //declare and initialize variables
  6.       int p1Score = 0, p2Score = 0;
  7.       Scanner kb = new Scanner(System.in);
  8.      
  9.       //Get first ball score
  10.       System.out.print("Enter a ball: ");
  11.       int ball = kb.nextInt();
  12.      
  13.       //while loop - keep going while neither player has 70 or more
  14.       while( p1Score <= 70 && p2Score <= 70 ){
  15.          
  16.          //Player 1's turn while ball is not 0
  17.          while( ball != 0 ){
  18.             p1Score += ball;
  19.             //Output new score
  20.             System.out.println("Player1\t" + p1Score + "\tPlayer2\t" + p2Score);
  21.             //Get next ball
  22.             System.out.print("Enter a ball: ");
  23.             ball = kb.nextInt();
  24.          }
  25.          
  26.          //Get first ball of player 2's turn
  27.          System.out.print("Enter a ball: ");
  28.          ball = kb.nextInt();
  29.          
  30.          //Player 2's turn while ball is not 0
  31.          while( ball != 0 ){
  32.             p2Score += ball;
  33.             //Output new score
  34.             System.out.println("Player1\t" + p1Score + "\tPlayer2\t" + p2Score);
  35.             //Get next ball
  36.             System.out.print("Enter a ball: ");
  37.             ball = kb.nextInt();
  38.          }
  39.       }
  40.      
  41.       //End of game if code reaches here
  42.       if( p1Score > p2Score ) System.out.print("Player1 wins " + p1Score + " to " + p2Score);
  43.       else if( p2Score > p1Score ) System.out.print("Player 2 wins " + p2Score + " to " + p1Score);
  44.       else System.out.print("It's a tie " + p1Score + " to " + p2Score);
  45.    }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement