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
andplayer2Score
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:
- Ensure you have the Java Development Kit (JDK) installed on your computer.
- Copy the above code into a file named
PingPongScoreboard.java
. - Open a terminal or command prompt and navigate to the directory containing the file.
- Compile the program using the following command:
javac PingPongScoreboard.java
- Run the program with:
java PingPongScoreboard
- Follow the on-screen prompts to interact with the game.