Design an application that will allow a user to keep track of the amount of tennis matches won by three professional tennis players. The user should be presented with a menu using the numbers 1, 2 and 3 that corresponds to the tennis players below. Tennis Players: 1. Novak Djokovic 2. Andy Murray 3. Roger Federer Prompt the user to choose a tennis player who has won a match from the menu using the numbers 1, 2 or 3; else the user enters 0 to quit. If the user chooses 1, 2 or 3, show the input section again and if the user then chooses 0, show a breakdown of the amount of matches won by each tennis player. Please note that menu items appear in the Console window. Menu items do not need to be created. Your output should also display the amount of games that have been captured. Make use of a method to get the input from the user and another method to increment the games won by the player. In your solution make provision for if a user has entered in an invalid tennis player number.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] count = new int[3];
boolean state = true;
while (state) {
System.out.println("[1] Novak Djokovic\n[2] Andy Murray\n[3] Roger Federer\n[0] Exit");
String text = scanner.next();
if (text == "0") System.out.printf("Novak Djokovic - %d\nAndy Murray - %d\nRoger Federer - %d\n\nProcess terminated", count[0], count[1], count[2]);
else if (text == "1") count[0]++;
else if (text == "2") count[1]++;
else if (text == "3") count[2]++;
else System.out.println("Invalid choice!");
}
}
}
Comments
Leave a comment