Guest User

JavaBasketball.java

a guest
Jul 8th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. /*
  2.  * Java Basketball
  3.  * by Clint
  4.  * 2020.07.09 | 8:43AM
  5.  * This Java program let's the player play basketball in console.
  6.  */
  7.  
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. public class JavaBasketball {
  12.  
  13.     static Scanner scanner = new Scanner(System.in);
  14.     static String enter;
  15.     static String playerInput;
  16.  
  17.  
  18.     // main method
  19.     public static void main(String[] args) {
  20.         System.out.println("WELCOME TO JAVA CONSOLE BASKETBALL\n" +
  21.                 "how to play:\n" +
  22.                 "press e or type enter to enter the game");
  23.         enter = scanner.nextLine();
  24.         switch (enter) {
  25.             case "e", "E", "enter", "ENTER", "Enter" -> shootBall();
  26.             default -> help();
  27.         }
  28.  
  29.         while (true) {
  30.             String playerInput = shootBall();
  31.         }
  32.     }
  33.  
  34.  
  35.     // help
  36.     private static void help() {
  37.         String help;
  38.  
  39.         System.err.println("Press e or type enter to play the game, dummy!");
  40.         System.out.println("HELP:\n" +
  41.                 "how to start the game:\n" +
  42.                 "press e or type enter to start the game\n\n" +
  43.                 "how to play the game:\n" +
  44.                 "press s or type shoot to shoot the ball");
  45.         help = scanner.nextLine();
  46.         switch (help) {
  47.             case "e", "E", "enter", "ENTER", "Enter" -> shootBall();
  48.             default -> help();
  49.         }
  50.     }
  51.  
  52.  
  53.     // shoot
  54.     private static String shootBall() {
  55.         System.out.print("SHOOT: ");
  56.         playerInput = scanner.nextLine();
  57.         switch (playerInput) {
  58.             case "s", "S", "shoot", "Shoot", "SHOOT" -> printResult();
  59.             default -> System.err.println("SHOOT THE BALL PROPERLY.\n");
  60.         }
  61.         return playerInput;
  62.     }
  63.  
  64.  
  65.     // result
  66.     private static void printResult() {
  67.         String[] shoot = {"1 POINT!", "2 POINTS!", "3 POINTS!", "MISSED!"};
  68.         Random random = new Random();
  69.         int shot = random.nextInt(shoot.length);
  70.         switch (playerInput) {
  71.             case "s", "S", "shoot", "SHOOT", "Shoot" -> System.out.println(shoot[shot] + "\n");
  72.             default -> System.err.println("SHOOT THE BALL PROPERLY.\n");
  73.         }
  74.     }
  75. }
Add Comment
Please, Sign In to add comment