Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Basketball{
- public static void main(String[] args){
- //Declare and initialize variables
- int homeScore = 0, awayScore = 0;
- Scanner kb = new Scanner(System.in);
- //Get first basket (in points)
- System.out.print("Enter basket: ");
- int basket = kb.nextInt();
- System.out.println("Home\tAway");
- //First half - Keep looping while output not 0
- while( basket != 0 ){
- //Positive basket so home team score
- if( basket > 0 ){
- if( basket <= 3 ) homeScore += basket;//add to home team score
- else System.out.println("INVALID SCORE");//more than 3 is invalid
- }
- //Negative basket so away team score
- else{
- if( basket >= -3 ) awayScore -= basket;//subtract from away team score (because negative so minus negative => plus a positive)
- else System.out.println("INVALID SCORE");//less than -3 is invalid
- }
- //Print current score and get next basket
- System.out.println( homeScore + "\t\t\t" + awayScore );
- System.out.print("Enter basket: ");
- basket = kb.nextInt();
- }
- //End of half so print message and get basket to start second half
- System.out.println("~**End of First Half**~");
- System.out.print("Enter basket: ");
- basket = kb.nextInt();
- System.out.println("Home\tAway");
- while( basket != 0 ){
- //Positive basket so home team score
- if( basket > 0 ){
- if( basket <= 3 ) homeScore += basket;//add to home team score
- else System.out.println("INVALID SCORE");//more than 3 is invalid
- }
- //Negative basket so away team score
- else{
- if( basket >= -3 ) awayScore -= basket;//subtract from away team score (because negative so minus negative => plus a positive)
- else System.out.println("INVALID SCORE");//less than -3 is invalid
- }
- //Print current score and get next basket
- System.out.println( homeScore + "\t\t\t" + awayScore );
- System.out.print("Enter basket: ");
- basket = kb.nextInt();
- }
- //end of game so print final score and winner
- System.out.println("~**End of Game**~");
- if( homeScore > awayScore ) System.out.println("Home team wins!");
- else if( awayScore > homeScore ) System.out.println("Away team wins.");
- else{//if tied, pick a random score and print winner
- homeScore = (int)(Math.random() * 100 + 1);
- awayScore = (int)(Math.random() * 100 + 1);
- System.out.println("~**TIE SO RANDOM SCORE**~");
- if( homeScore > awayScore ) System.out.println("Home team wins!");
- else if( awayScore > homeScore ) System.out.println("Away team wins.");
- }
- //Print final score no matter who won
- System.out.println("Home\tAway");
- System.out.println(homeScore + "\t\t\t" + awayScore);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement