Tablero de Ajedrez, método que retorna un valor tipo Object, con ArrayList

package matrices; import java.util.*; /** * @author GALIA * @version noviembre 2014 Clase que imprime las casillas de un tablero de * ajedrez */ public class Ajedrez { /** * @return tablero */ public static Object[] imprimeTablero() { // creamos una matriz, en este caso de 8 por 8 ArrayList<Integer> tablero = new ArrayList<Integer>(); // el bucle externo recorre las filas,el interno las columnas for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { // si la casilla es par,es blanca;si impar-negra String casilla = ((i + j) % 2 == 0) ? "blanco " : "negro "; // el operador ternario evita los if-else System.out.print(" " + casilla + " "); } System.out.println();// hace el salto de línea } return tablero.toArray(); // por eso el tipo de retorno es Object } public static void main(String[] args) { Arrays.deepToString(imprimeTablero()); // imprimeTablero();//hacen lo mismo } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.