Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Java Basketball
- * by Clint
- * 2020.07.09 | 8:43AM
- * This Java program let's the player play basketball in console.
- */
- import java.util.Random;
- import java.util.Scanner;
- public class JavaBasketball {
- static Scanner scanner = new Scanner(System.in);
- static String enter;
- static String playerInput;
- // main method
- public static void main(String[] args) {
- System.out.println("WELCOME TO JAVA CONSOLE BASKETBALL\n" +
- "how to play:\n" +
- "press e or type enter to enter the game");
- enter = scanner.nextLine();
- switch (enter) {
- case "e", "E", "enter", "ENTER", "Enter" -> shootBall();
- default -> help();
- }
- while (true) {
- String playerInput = shootBall();
- }
- }
- // help
- private static void help() {
- String help;
- System.err.println("Press e or type enter to play the game, dummy!");
- System.out.println("HELP:\n" +
- "how to start the game:\n" +
- "press e or type enter to start the game\n\n" +
- "how to play the game:\n" +
- "press s or type shoot to shoot the ball");
- help = scanner.nextLine();
- switch (help) {
- case "e", "E", "enter", "ENTER", "Enter" -> shootBall();
- default -> help();
- }
- }
- // shoot
- private static String shootBall() {
- System.out.print("SHOOT: ");
- playerInput = scanner.nextLine();
- switch (playerInput) {
- case "s", "S", "shoot", "Shoot", "SHOOT" -> printResult();
- default -> System.err.println("SHOOT THE BALL PROPERLY.\n");
- }
- return playerInput;
- }
- // result
- private static void printResult() {
- String[] shoot = {"1 POINT!", "2 POINTS!", "3 POINTS!", "MISSED!"};
- Random random = new Random();
- int shot = random.nextInt(shoot.length);
- switch (playerInput) {
- case "s", "S", "shoot", "SHOOT", "Shoot" -> System.out.println(shoot[shot] + "\n");
- default -> System.err.println("SHOOT THE BALL PROPERLY.\n");
- }
- }
- }
Add Comment
Please, Sign In to add comment