import java.util.Arrays;
class Diamante {
private static final String[] ALFABETO
= {"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"};
public static void montar
(String escolhida
) { int maximo
= Arrays.
binarySearch(ALFABETO, escolhida
); // Encontra a posição da letra passada como parâmetro boolean par = (maximo % 2) == 0; // Verifica se o número é par ou impar para cálculo da posição
int metade
= Math.
floorDiv(maximo,
2); // Encontra a posição onde ficará o meio do diamante String espacos
= " ".
substring(0, metade
); //Corta o número de espaços para realizar o espelhamento int posicao = 0; // Inicia o contador de posição
for (int indice = 0; indice <= maximo; indice++) {
String letra
= ALFABETO
[indice
]; StringBuilder construtor = new StringBuilder(espacos); // Monta um StringBuilder para substituição das letras
// Se for a primeira posição ou a última não espelha o texto
if (posicao == 0) {
texto = espacos + letra + espacos;
} else {
construtor.setCharAt(posicao - 1, letra.charAt(0)); // Coloca a letra na posição correta dentro da String de espaços
texto = construtor.toString();
texto = inverter(texto) + " " + texto; // Inverte o texto obtido criando espelhamento
}
System.
out.
println(texto
); // Imprime na tela
if (indice < metade) { // Caso ainda não tenha passado da metade, coloca mais espaços
posicao++;
} else if (par || indice > metade) { // Caso tenha passado da metade ou seja par e esteja na metade começa a remover espaços
posicao--;
}
}
}
StringBuilder construtor = new StringBuilder(texto);
return construtor.reverse().toString();
}
public static void main
(String[] args
) { montar("H");
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgpjbGFzcyBEaWFtYW50ZSB7CgogIHByaXZhdGUgc3RhdGljIGZpbmFsIFN0cmluZ1tdIEFMRkFCRVRPID0geyJBIiwgIkIiLCAiQyIsICJEIiwgIkUiLCAiRiIsICJHIiwgIkgiLCAiSSIsICJKIiwgIksiLCAiTCIsICJNIiwgIk4iLCAiTyIsICJQIiwgIlEiLCAiUiIsICJTIiwgIlQiLCAiVSIsICJWIiwgIlciLCAiWCIsICJZIi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