Thursday 4 April 2019

java - How can I print a pyramid of *

For example, enter a 5 and show this:










* * * * *     
* * *
*


My current code is this:

public static void main(String[] args) {
Scanner in = new Scanner(System.in);



    int num = 4;

while(num % 2 == 0){

System.out.println("Introduce un numero impar:");
num = in.nextInt();


if (num % 2 == 0){
System.out.println("Has introducido un numero par");
}
}

for (int numFila = 0; numFila < num; numFila++) { //filas
for (int numColumna = 0; numColumna < num*2-1; numColumna++) { //columnas

if (numFila <= numColumna){
System.out.print(" * ");

}else{
System.out.print(" ");
}

}
//Salt de línia per començar un altre fila
System.out.println();
}
}



And what I can show is the following:









* * * * * * *   
* * * * * *
* * * * *

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...