Iteration with Soccer Players 2D Array
class playerLoop {
// The area between class definition and the 1st method is where we keep data for object in Java
String [][] players;
/**
* Constructor initializes a 2D array of s
*/
public playerLoop() {
//Storing Data in 2D arrays
players = new String[][]{
//Player 0
{
" o__",
"/| ",
"/ > o"
},
//player 1
{
" o__",
"/| ",
"/ > o"
},
//player 2
{
" o__",
"/| ",
"/ > o"
},
//player 3
{
" o__",
"/| ",
"/ > o"
},
//player 4
{
" o__",
"/| ",
"/ > o"
},
};
}
/**
* Loop and print s in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("The Great Story of the Players Who Shot A Soccer Ball");
System.out.println("---------------------------------");
// players (non-primitive) defined in constructor knows its length
int playerCount = players.length;
for (int i = playerCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of players
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " players shooting the ball...");
//how many separate parts are there in a player player?
int partCount = players[0].length;
for (int row = 0; row < partCount; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each player part by part, will eventually print entire column*/
for (int col = 0; col < playerCount; col++) {
// prints specific part of the player from the column
System.out.print(players[col][row] + " ");
//this is new line between separate parts
}
//this new line gives separation between stanza of poem
System.out.println();
}
//countdown for poem, decrementing playerCount variable by 1
System.out.println("One missed and got benched");
playerCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("No more players shooting in the field");
System.out.println("------------------------------------");
System.out.println(" THE END ");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new playerLoop().printPoem(); //a new player list and output in one step
}
}
playerLoop.main(null);