Created
August 4, 2019 03:03
-
-
Save wagfim/8bf5774040734009521f667f48de114d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* @author Wagner Bonfim | |
*/ | |
public class Questao8 { | |
public static void main(String[] args) { | |
Scanner leitor = new Scanner(System.in); | |
int[][] matriz = { | |
{0,0,0,0,0,0}, | |
{0,0,0,0,0,0}, | |
{0,0,0,0,0,0}, | |
{0,0,0,0,0,0} | |
}; | |
exibeMatrizNumerado(matriz); | |
System.out.print("linha: "); | |
int linha = leitor.nextInt()-1; | |
System.out.print("coluna: "); | |
int coluna = leitor.nextInt()-1; | |
matriz = marcarElemento(matriz, linha, coluna); | |
System.out.println(); | |
exibeMatriz(matriz); | |
} | |
public static int[][] marcarElemento(int[][] matriz, int linha, int coluna) { | |
if (coluna > 0) //oeste | |
matriz[linha][coluna-1] = 1; | |
if (coluna > 0 && linha > 0) //noroeste | |
matriz[linha-1][coluna-1] = 1; | |
if (coluna > 0 && linha < matriz.length-1) //sudoeste | |
matriz[linha+1][coluna-1] = 1; | |
if (coluna < matriz[linha].length-1) //leste | |
matriz[linha][coluna+1] = 1; | |
if (coluna < matriz[linha].length-1 && linha > 0) //nordeste | |
matriz[linha-1][coluna+1] = 1; | |
if (coluna < matriz[linha].length-1 && linha < matriz.length-1) //sudeste | |
matriz[linha+1][coluna+1] = 1; | |
if (linha > 0) //norte | |
matriz[linha-1][coluna] = 1; | |
if (linha < matriz.length-1) //sul | |
matriz[linha+1][coluna] = 1; | |
return matriz; | |
} | |
public static void exibeMatrizNumerado(int[][] matriz) { | |
System.out.print(" "); | |
for (int i = 0; i < matriz[0].length; i++) { | |
System.out.print((i+1)+" "); | |
} | |
System.out.println(); | |
for (int i = 0; i < matriz.length; i++) { | |
System.out.print((i+1)+" "); | |
for (int j = 0; j < matriz[i].length; j++) { | |
System.out.print(matriz[i][j]+" "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void exibeMatriz(int[][] matriz) { | |
for (int i = 0; i < matriz.length; i++) { | |
for (int j = 0; j < matriz[i].length; j++) { | |
System.out.print(matriz[i][j]+" "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment