Java - triangulo invertido

 
Vista:

triangulo invertido

Publicado por jose (1 intervención) el 16/01/2008 14:54:33
alguien que me eche una mano aunque sea al cuello, he empezado hace muy poquito en la carrera conla programación y he desarrollado este algoritmo, la idea es que genere un triangulo invertido dada una altura tal que asi

para h=3

* + * + *
* - *
*

con los "+" y los "-", el codigo que he desarrolado es:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Triangulo_Invertido {


public static void main(String[] args)throws IOException {

InputStreamReader teclado;
BufferedReader bufferLectura;
String linea;
teclado = new InputStreamReader (System.in);
bufferLectura = new BufferedReader (teclado);
System.out.print("Introduzca una altura impar: ");
linea = bufferLectura.readLine ();
int h = Integer.parseInt (linea);

int h2 = h;
int simbolo = h-1;

if (h%2<=0) {
System.out.print("Mejor que sea impar");
}

else {

int espacios=0;
while (h2>=1^(espacios<h)^(simbolo>=0)); {
int x=h2;
int y=h2-1;
if (x%2!=0) {
while (espacios>0)
System.out.print(" ");
espacios=espacios-1;
while ((x>0)^(y>=0)) {
if (x>0);
System.out.print("*");
x=x-1;
if (y>0);
System.out.print("+");
y=y-1;
}}
else {
while (espacios>0)
System.out.print(" ");
espacios=espacios-1;
while ((x>0)^(y>=0)) {
if (x>0);
System.out.print("*");
x=x-1;
if (y>0);
System.out.print("-");
y=y-1;
}
System.out.println();
h2=h2-1;
espacios=h-h2;
simbolo=simbolo-1;
}
}
}




}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder