Question: How Many Players You have been provided with a Stringvariable called sport and an int variable called players. Write a switch statement that will reassignthe

How Many Players
You have been provided with a Stringvariable called sport and an int variable called players.
Write a switch statement that will reassignthe value of players based on the sportprovided. The sport input can be either uppercase or lowercase, so please keep this in mind when writing your code.
The following sports should be considered along with the corresponding number of players:
Soccer (11 players)
Basketball (5 players)
Baseball (9 players)
Hockey (6 players)
Tennis (1 player)
If the sport variable matches one of the sports listed above, reassign players to the corresponding number of players. You do not need to print or return any code. The print statements are provided for you.
If the sport variable does not match any of the listed sports, use the default case to reassign players to the value -1.
import java.util.*;
public class SportsPlayers {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String sport = input.nextLine();
int players =-1;
/***** DO NOT CHANGE THE CODE ABOVE THIS LINE *****/
// WRITE SWITCH STATEMENT HERE
/***** DO NOT CHANGE THE CODE BELOW THIS LINE *****/
if (players ==-1){
System.out.println("Unknown sport");
} else {
System.out.println("In "+ sport +", there are "+ players +" players on each team.");
}
}
}
STDOUT
Expected STDOUT
Insoccer,thereare11playersoneachteam.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!