fork download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4.  
  5. public static void main(String[] args) {
  6. // Scanner scanner = new Scanner(System.in);
  7. // System.out.print("Enter an integer: ");
  8. // int n = scanner.nextInt();
  9. // printPattern(n);
  10. // scanner.close();
  11.  
  12. printPattern(3);
  13. }
  14.  
  15. public static void printPattern(int n) {
  16. int totalRows = n + 2;
  17. int totalCols = n * 2;
  18.  
  19. for (int i = 1; i <= totalRows; i++) {
  20. if (i <= n) { // Top n rows
  21. for (int j = 1; j <= totalCols; j++) {
  22. if (j <= totalCols - n) {
  23. System.out.print(" ");
  24. } else {
  25. System.out.print("e");
  26. }
  27. }
  28. } else if (i == n + 1) { // Second to last row with asterisk
  29. for (int j = 1; j <= totalCols; j++) {
  30. int starStartCol = totalCols - n - (n / 2) - 1;
  31. int starEndCol = starStartCol + (2 * (n / 2) + 1);
  32.  
  33. if (j >= starStartCol && j <= starEndCol) {
  34. System.out.print("*");
  35. } else if (j > totalCols - n) {
  36. System.out.print("e");
  37. } else {
  38. System.out.print(" ");
  39. }
  40. }
  41. } else { // Last row with all asterisks
  42. for (int j = 1; j <= totalCols; j++) {
  43. if (j <= totalCols - n) {
  44. System.out.print("*");
  45. } else {
  46. System.out.print("e");
  47. }
  48. }
  49. }
  50. System.out.println();
  51. }
  52. }
  53. }
Success #stdin #stdout 0.12s 54604KB
stdin
Standard input is empty
stdout
   eee
   eee
   eee
****ee
***eee