Advertisement
fosterbl

Basketball.java

Oct 4th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Basketball{
  4.    public static void main(String[] args){
  5.       //Declare and initialize variables
  6.       int homeScore = 0, awayScore = 0;
  7.       Scanner kb = new Scanner(System.in);
  8.      
  9.       //Get first basket (in points)
  10.       System.out.print("Enter basket: ");
  11.       int basket = kb.nextInt();
  12.       System.out.println("Home\tAway");
  13.      
  14.       //First half - Keep looping while output not 0
  15.       while( basket != 0 ){
  16.      
  17.          //Positive basket so home team score
  18.          if( basket > 0 ){
  19.             if( basket <= 3 ) homeScore += basket;//add to home team score
  20.             else System.out.println("INVALID SCORE");//more than 3 is invalid
  21.          }
  22.          
  23.          //Negative basket so away team score
  24.          else{
  25.             if( basket >= -3 ) awayScore -= basket;//subtract from away team score (because negative so minus negative => plus a positive)
  26.             else System.out.println("INVALID SCORE");//less than -3 is invalid
  27.          }
  28.          
  29.          //Print current score and get next basket
  30.          System.out.println( homeScore + "\t\t\t" + awayScore );
  31.          System.out.print("Enter basket: ");
  32.          basket = kb.nextInt();
  33.       }
  34.      
  35.       //End of half so print message and get basket to start second half
  36.       System.out.println("~**End of First Half**~");
  37.       System.out.print("Enter basket: ");
  38.       basket = kb.nextInt();
  39.       System.out.println("Home\tAway");
  40.      
  41.       while( basket != 0 ){
  42.      
  43.          //Positive basket so home team score
  44.          if( basket > 0 ){
  45.             if( basket <= 3 ) homeScore += basket;//add to home team score
  46.             else System.out.println("INVALID SCORE");//more than 3 is invalid
  47.          }
  48.          
  49.          //Negative basket so away team score
  50.          else{
  51.             if( basket >= -3 ) awayScore -= basket;//subtract from away team score (because negative so minus negative => plus a positive)
  52.             else System.out.println("INVALID SCORE");//less than -3 is invalid
  53.          }
  54.          
  55.          //Print current score and get next basket
  56.          System.out.println( homeScore + "\t\t\t" + awayScore );
  57.          System.out.print("Enter basket: ");
  58.          basket = kb.nextInt();
  59.       }
  60.      
  61.       //end of game so print final score and winner
  62.       System.out.println("~**End of Game**~");
  63.       if( homeScore > awayScore ) System.out.println("Home team wins!");
  64.       else if( awayScore > homeScore ) System.out.println("Away team wins.");
  65.       else{//if tied, pick a random score and print winner
  66.          homeScore = (int)(Math.random() * 100 + 1);
  67.          awayScore = (int)(Math.random() * 100 + 1);
  68.          System.out.println("~**TIE SO RANDOM SCORE**~");
  69.          if( homeScore > awayScore ) System.out.println("Home team wins!");
  70.          else if( awayScore > homeScore ) System.out.println("Away team wins.");
  71.       }
  72.       //Print final score no matter who won
  73.       System.out.println("Home\tAway");
  74.       System.out.println(homeScore + "\t\t\t" + awayScore);
  75.    }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement