Thursday, April 4, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...