fork download
  1. class Ideone {
  2. public static void main(String[] args) {
  3. int n = 5; // You can change n to any odd number
  4. if (n % 2 == 1) {
  5. printPattern(n);
  6. } else {
  7. System.out.println("The input number must be odd.");
  8. }
  9. }
  10.  
  11. public static void printPattern(int n) {
  12. int totalRows = n + 2;
  13. int rowLength = n + (n + 1) / 2;
  14.  
  15. // Print the first n rows
  16. for (int i = 0; i < n; i++) {
  17. StringBuilder row = new StringBuilder();
  18. for (int j = 0; j < rowLength - (n + 1) / 2; j++) {
  19. row.append(" "); // g means space
  20. }
  21. for (int j = 0; j < (n + 1) / 2; j++) {
  22. row.append("e");
  23. }
  24. System.out.println(row.toString());
  25. }
  26.  
  27. // Print the last 2 rows
  28. // Row n+1
  29. int stars = n / 2;
  30. int leadingSpaces = (n + 1) / 2 - stars;
  31. StringBuilder rowNPlus1 = new StringBuilder();
  32. for (int j = 0; j < leadingSpaces; j++) {
  33. rowNPlus1.append(" ");
  34. }
  35. for (int j = 0; j < stars; j++) {
  36. rowNPlus1.append("*");
  37. }
  38. for (int j = 0; j < (n + 1) / 2; j++) {
  39. rowNPlus1.append("e");
  40. }
  41. System.out.println(rowNPlus1.toString());
  42.  
  43. // Row n+2
  44. StringBuilder rowNPlus2 = new StringBuilder();
  45. for (int j = 0; j < n; j++) {
  46. rowNPlus2.append("*");
  47. }
  48. for (int j = 0; j < (n + 1) / 2; j++) {
  49. rowNPlus2.append("e");
  50. }
  51. System.out.println(rowNPlus2.toString());
  52. }
  53. }
  54.  
Success #stdin #stdout 0.1s 52724KB
stdin
Standard input is empty
stdout
     eee
     eee
     eee
     eee
     eee
 **eee
*****eee