4
\$\begingroup\$

I made a simple console basketball game using Java.

/*
 * Java Basketball
 * by Clint
 * 2020.07.08 | 3:59AM
 * This Java program let's the player play basketball in console.
 */
 
import java.util.Random;
import java.util.Scanner;

public class Basketball {

    static String[] shoot = {"1 POINT!", "2 POINTS!", "3 POINTS!", "MISSED!"};
    static String player;
    
    public static void main(String[] args) {
        System.out.println("WELCOME TO JAVA CONSOLE BASKETBALL\n" +
                "how to play:\n" +
                "press \"s\" or type \"shoot\" to shoot the basketball.\n");
                
        while (true) {
            shootBall();
        }
    }

    public static void shootBall() {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        
        int shot = random.nextInt(shoot.length);
        System.out.print("SHOOT: ");
        player = scanner.nextLine();
        
        switch (player) {
            case "s", "S", "shoot", "Shoot", "SHOOT" -> System.out.println(shoot[shot] + "\n");
            default -> System.out.println("SHOOT THE BALL PROPERLY.\n");
        }
    }
}

ps: ik that there's while (true) in my code, but it's the thing that I know so far that keeps the programme running. I'm open for suggestions on how to improve my coding.

\$\endgroup\$
4
\$\begingroup\$

Descriptive variable names

You have this:

player = scanner.nextLine();

Does the input reflect a player?

I think a more descriptive name would be playerInput. That makes the switch more readable as well.

Make methods do one thing (single responsibility principle)

Your shootBall method does three things: asking for input, calculating the result, and printing the result. Ideally this would be split up:

  • private String getInput()
  • private String shootBall(String input)
  • private String printResult(String result)

This makes it easier to change the implementation of a responsibility and easier to read the logic of your program.

The main while loop would become

while(true) {
    String input = getInput();
    String result = shootBall(input);
    printResult(result);
}

This makes it easier for coding some exit value as well;

while (true) {
    String input = getInput();
    if (input.equals("QUIT"))
      break;
    String result = shootBall(input);
    printResult(result);
} 

Or, if you don't like the while-true-break

String input;
while (!(input = getInput()).equals("QUIT")) {
    printResult(shootBall(input));
} 
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ if (input.equals("QUIT")) - i thought you meant: if(isQuitCommand(input)) ... nice answer +1 \$\endgroup\$ – Martin Frank Jul 8 at 11:07
  • 2
    \$\begingroup\$ @MartinFrank yes, good suggestion! \$\endgroup\$ – RobAu Jul 8 at 11:15
  • \$\begingroup\$ Thank you for the help. I made different methods for different parts of the program: pastebin.com/WfGrQtdV, how was it? \$\endgroup\$ – fosionef Jul 9 at 1:29
  • 1
    \$\begingroup\$ You should look into passing variables as method parameters :) \$\endgroup\$ – RobAu Jul 9 at 7:07
2
\$\begingroup\$

You only need one instance of Random and Scanner for the whole programm. Especially creating new instances of Scanner is bad, because it locks resources, that you never free by closing it.

For the switch you are unnecessarily using the new expression syntax which is used when you want to return a value which you don't. The more conventional syntax would be:

switch (player) {
  case "s", "S", "shoot", "Shoot", "SHOOT":
    System.out.println(shoot[shot] + "\n");
    break;
  default:
    System.out.println("SHOOT THE BALL PROPERLY.\n");
    break;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.