About JAVA Programming Imagine that two baseball teams, Rangers and Yankees, pla
ID: 3564080 • Letter: A
Question
About JAVA Programming
Imagine that two baseball teams, Rangers and Yankees, play in the World Series Championship games this year. They will play 7 games. Write a Java program that can store the imagined scores (home runs) of each team at the end of the first official nine innings of each game of the all 7 games. The program reads the scores of each team from the console and stored them in arrays. At the end, the program displays (to the console) the result of each game (for all 7 games), i.e. which team is winning or the teams tie at the end of the 9th inning.
Sample of the output:
Game 1: Rangers wins
Game 2: Yankees wins.
Game 3: Both the teams tied at the end of the 9th inning.
Explanation / Answer
import java.util.Scanner;
public class match {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int games=7;
int innings=9;
int[] Rangers=new int[9];
int[] Yankees=new int[9];
String[] result=new String[7];
for(int i=0;i<games;i++){
int YankeesTotal=0;
int RangersTotal=0;
System.out.println("Please enter the scores for match "+(i+1));
System.out.print("Rangers ==> ");
for(int j=0;j<9;j++){
Rangers[j]=input.nextInt();
RangersTotal+=Rangers[j];
}
System.out.print("Yankees ==> ");
for(int j=0;j<9;j++){
Yankees[j]=input.nextInt();
YankeesTotal+=Yankees[j];
}
if(YankeesTotal>RangersTotal){
result[i]="Game "+i+" Yankees wins";
}
else{
result[i]="Game "+i+" Rangers wins";
}
}
for(int k=0;k<result.length;k++){
System.out.println(result[k]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.