I'm computing a star pattern with the variable num_star. This num_star (greater than 2) determines the maximum number of characters.
No need for complications, just simple logic and for-loop as I'm learning.
My logic: Odd number prints 1 star first. Even number prints 2 stars first.
Example: num_star = 3
*
***
***
*
num_star = 4
**
****
****
**
num_star = 5
*
*****
***
*****
*
num_star = 6
**
******
****
******
**
num_star = 7
*
***
*******
*****
*******
***
*
num_star = 8
**
****
********
******
********
****
**
num_star = 9
*
***
*********
*******
*****
*******
*********
***
*
I've tried the Triangle method on Google but I have no idea on how to compute/logic those in the middle..
My code:
int num_star = 3;
if (num_star%2==1) {
for(int i = 1; i< num_star; i++){
for(int j = i; j< num_star; j++){
System.out.print(" ");
}
for(int k = 1; k<(2*i); k++){
System.out.print("*");
}
System.out.println();
}
for (int i = num_star-1; i > 0; i--){
for (int j = num_star; j > i; j--){
System.out.print(" ");
}
for (int k = (2*i); k > 1; k--){
System.out.print("*");
}
System.out.println();
}
}
Aucun commentaire:
Enregistrer un commentaire