Java
2. [P2_SluggingPctComputer] You are now working for baseball statistics company in need of a new program for computing a baseball batter's slugging percentage.
A batter's slugging percentage is based on the number of opportunities they have to hit the baseball, also called their at bats, and how many bases they touched at each of those at bats. For example, if a baseball player is at bat and they hit the ball for a double, this indicates they touched two bases. If the player gets a home run, this indicates they get to touch four bases, the maximum possible. If the player is at bat and they do not get a hit, then they touch 0 bases. We then compute the slugging percentage as the total number of bases the player touched divided by the number of at bats they had.
Your program will prompt a user to enter the number of bases touched for each of a player's at bats (i.e., 0, 1, 2, 3, or 4) until they enter a -1 to stop entering bases touched. Then the program should report the total number of at bats entered and the slugging percentage. For example:
|
This program computes the slugging percentage for a baseball batter. You will need to enter the number of bases for each of the batter's at bats. Please enter: 0 for no hit 1 for a single 2 for a double 3 for a triple 4 for a home run -1 to quit and get the slugging percentage Enter the bases touched for the next at bat: 1 Enter the bases touched for the next at bat: 2 Enter the bases touched for the next at bat: 3 Enter the bases touched for the next at bat: 4 Enter the bases touched for the next at bat: 0 Enter the bases touched for the next at bat: -1 Total at bats: 5 Slugging Pct: 2.000
[WELCOME AND INSTRUCTIONS...] Enter the bases touched for the next at bat: 0 Enter the bases touched for the next at bat: 0 Enter the bases touched for the next at bat: 1 Enter the bases touched for the next at bat: 1 Enter the bases touched for the next at bat: 2 Enter the bases touched for the next at bat: 3 Enter the bases touched for the next at bat: 4 Enter the bases touched for the next at bat: 0 Enter the bases touched for the next at bat: -1 Total at bats: 8 Slugging Pct: 1.375
|
Where I can do them in Eclipse: