Java

 

Welcome to the digital scoreboard for a Ping Pong game. This simple Java program will help you track the score of two players during a game. With this, you can easily manage and display scores as the game progresses.

Objective

The main objective of this program is to create a Ping Pong scoreboard that displays the current scores of two players, handles score increments, and provides a simple way to reset the scores. This program allows users to interact with the scoreboard through a graphical user interface (GUI) or terminal-based interaction.

Program Code


import java.util.Scanner;

public class PingPongScoreboard {
    private int player1Score = 0;
    private int player2Score = 0;

    // Method to display the current scores
    public void displayScores() {
        System.out.println("Player 1 Score: " + player1Score);
        System.out.println("Player 2 Score: " + player2Score);
    }

    // Method to increment Player 1's score
    public void player1Scores() {
        player1Score++;
        System.out.println("Player 1 scores! Current Score:");
        displayScores();
    }

    // Method to increment Player 2's score
    public void player2Scores() {
        player2Score++;
        System.out.println("Player 2 scores! Current Score:");
        displayScores();
    }

    // Method to reset the scores
    public void resetScores() {
        player1Score = 0;
        player2Score = 0;
        System.out.println("Scores have been reset!");
        displayScores();
    }

    public static void main(String[] args) {
        PingPongScoreboard scoreboard = new PingPongScoreboard();
        Scanner scanner = new Scanner(System.in);
        String userInput;

        // Game loop
        while (true) {
            System.out.println("\nEnter '1' for Player 1 to score, '2' for Player 2, 'r' to reset scores, or 'q' to quit:");
            userInput = scanner.nextLine();

            if (userInput.equals("1")) {
                scoreboard.player1Scores();
            } else if (userInput.equals("2")) {
                scoreboard.player2Scores();
            } else if (userInput.equals("r")) {
                scoreboard.resetScores();
            } else if (userInput.equals("q")) {
                System.out.println("Game Over. Final Scores:");
                scoreboard.displayScores();
                break;
            } else {
                System.out.println("Invalid input. Please try again.");
            }
        }

        scanner.close();
    }
}
        

Program Explanation and How to Run

This Java program simulates a Ping Pong scoreboard. Here’s how the program works:

  • Variables: The program uses two variables player1Score and player2Score to track the score of each player.
  • Methods:
    • displayScores() displays the current score of both players.
    • player1Scores() increments the score of Player 1 by 1.
    • player2Scores() increments the score of Player 2 by 1.
    • resetScores() resets both players’ scores to 0.
  • Game Flow: The main method contains a game loop that continuously asks the user to input commands. Players can score by typing ‘1’ for Player 1, ‘2’ for Player 2, ‘r’ to reset the scores, or ‘q’ to quit the game.

Steps to Run the Program:

  1. Ensure you have the Java Development Kit (JDK) installed on your computer.
  2. Copy the above code into a file named PingPongScoreboard.java.
  3. Open a terminal or command prompt and navigate to the directory containing the file.
  4. Compile the program using the following command: javac PingPongScoreboard.java
  5. Run the program with: java PingPongScoreboard
  6. Follow the on-screen prompts to interact with the game.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)