-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiamondCharacterPattern.java
More file actions
63 lines (63 loc) · 1.7 KB
/
DiamondCharacterPattern.java
File metadata and controls
63 lines (63 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
1. import java.util.Scanner;
2. public class DiamondCharacterPattern
3. {
4. public static void main(String[] args)
5. {
6. char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
7. int alphabet _number = 0;
8. String[] diamond = new String[26]; // array of strings
9. System.out.print("Enter a Character between A to Z : ");
10. Scanner reader = new Scanner(System.in);
11. try
12. {
13. char user_ alphabet = reader.next("[A-Z]").charAt(0);
14. // search for letter number in the array letter
15. for (int i = 0; i < alphabet.length; i++)
16. {
17. if (letter[i] == user_ alphabet)
18. {
19. alphabet _number = i;
20. break;
21. }
22. }
23. //construct diamond
24. for (int i = 0; i <= alphabet _number; i++)
25. {
26. diamond[i] = "";
27. //add initial spaces
28. for (int j = 0; j < alphabet _number - i; j++)
29. {
30. diamond[i] += " ";
31. }
32. // add alphabet
33. diamond[i] += alphabet
34. //add space between letters
35. if (alphabet[i] != 'A')
36. {
37. for (int j = 0; j < 2 * i - 1; j++)
38. {
39. diamond[i] += " ";
40. }
41. // add alphabet
42. diamond[i] += alphabet[i];
43. }
44. // Draw the first part of the diamond
45. System.out.println(diamond[i]);
46. }
47. for (int i = alphabet _number - 1; i >= 0; i--)
48. {
49. // Draw the second part of the diamond
50. // prints the diamondArray in the reverse order
51. System.out.println(diamond[i]);
52. }
53. }
54. catch (Exception e)
55. {
56. e.printStackTrace();
57. }
58. finally
59. {
60. reader.close();
61. }
62. }
63. }