fork download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4.  
  5. public static void printPattern(int n) {
  6. if (n % 2 == 0) {
  7. System.out.println("Input must be an odd number.");
  8. return;
  9. }
  10.  
  11. int totalWidth = n * 2;
  12. int midPoint = totalWidth / 2;
  13.  
  14. // Part 1: Top section of 'g' characters.
  15. // This prints n lines of 'g's and 'e's.
  16. for (int i = 0; i < n; i++) {
  17. for (int j = 0; j < totalWidth; j++) {
  18. if (j < n + 1) {
  19. System.out.print("g");
  20. } else {
  21. System.out.print("e");
  22. }
  23. }
  24. System.out.println();
  25. }
  26.  
  27. // Part 2: Middle line with a special character.
  28. // This prints a line with 'g's, one special character (*), and 'e's.
  29. for (int j = 0; j < totalWidth; j++) {
  30. if (j == n) {
  31. System.out.print("*");
  32. } else if (j < n + 1) {
  33. System.out.print("g");
  34. } else {
  35. System.out.print("e");
  36. }
  37. }
  38. System.out.println();
  39.  
  40. // Part 3: Bottom section of special characters.
  41. // This prints a line with a block of '*'s and a block of 'e's.
  42. for (int j = 0; j < totalWidth; j++) {
  43. if (j < n) {
  44. System.out.print("*");
  45. } else {
  46. System.out.print("e");
  47. }
  48. }
  49. System.out.println();
  50. }
  51.  
  52. public static void main(String[] args) {
  53. // Scanner scanner = new Scanner(System.in);
  54. // System.out.print("Enter an odd number: ");
  55. // int n = scanner.nextInt();
  56. // printPattern(n);
  57. // scanner.close();
  58.  
  59. printPattern(3);
  60. }
  61. }
Success #stdin #stdout 0.07s 54716KB
stdin
Standard input is empty
stdout
ggggee
ggggee
ggggee
ggg*ee
***eee