Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

baseball

Language/Type: Java if/else input Scanner while loops
Author: Marty Stepp (on 2010/12/28)

Write a static method named baseball that takes a Scanner representing the console as a parameter and plays an interactive baseball scoring game with the user, returning an integer representing which team won the game.

Baseball is a sport where teams play a series of innings against each other. Each team scores a certain number of runs (points) in each inning. A baseball game ends when one of the following conditions is met:

  • After 9 innings are finished, if one team has more total runs than the other, the team with more runs wins.
  • After 9 innings are finished, if the teams are tied (if they have the same number of total runs), we keep playing one more full inning at a time until the teams are not tied any more.
  • After any inning, if one team is ahead by 10 or more runs, the game is called and the team with more runs wins.

Your method should repeatedly prompt the user, once per inning, to enter the number of runs that each team scored during each inning. The user will type in the first team's runs for that inning, followed by the second team's runs for that inning, separated by whitespace. Your method should stop prompting once one or more of the above bulleted conditions are met, causing the game to end. At the end of the game, you should print the final score. You should also return an integer for which team won: return 1 if the first team won, and 2 if the second team won.

You may assume that the user enters valid non-negative integers whenever prompted, and you may assume that the game will eventually end (it won't go on forever).

Here are some example calls to the method and their resulting output and return values:

Scanner console = new Scanner(System.in);
Call
// typical game
baseball(console);
// 10 run difference; game called
baseball(console);
// extra innings
baseball(console);
Example
Output /
User Input
Inning #1: 1 1
Inning #2: 0 0
Inning #3: 2 1
Inning #4: 0 2
Inning #5: 1 0
Inning #6: 1 1
Inning #7: 3 1
Inning #8: 0 0
Inning #9: 1 0
Final score: 9 - 6
Inning #1: 0 1
Inning #2: 1 2
Inning #3: 0 3
Inning #4: 1 1
Inning #5: 0 4
Inning #6: 1 2
Final score: 3 - 13
Inning #1: 0 1
Inning #2: 1 0
Inning #3: 0 3
Inning #4: 1 0
Inning #5: 2 0
Inning #6: 1 2
Inning #7: 1 0
Inning #8: 0 0
Inning #9: 1 1
Inning #10: 0 0
Inning #11: 0 1
Final score: 7 - 8
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.