Spaces:
Running
Running
Create 22. Letter pyramid
Browse files
Week 2: Methods, strings and lists/22. Letter pyramid
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a program that asks the user to enter the lower case letter [a-z].
|
| 2 |
+
The program will then print a pyramid corresponding to the letter according to the example printouts shown below.
|
| 3 |
+
|
| 4 |
+
Example execution:
|
| 5 |
+
Give a lowercase letter: d
|
| 6 |
+
aa
|
| 7 |
+
baab
|
| 8 |
+
cbaabc
|
| 9 |
+
dcbaabcd
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Another example execution:
|
| 13 |
+
Give a lowercase letter: p
|
| 14 |
+
aa
|
| 15 |
+
baab
|
| 16 |
+
cbaabc
|
| 17 |
+
dcbaabcd
|
| 18 |
+
edcbaabcde
|
| 19 |
+
gedcbaabcdeg
|
| 20 |
+
hgedcbaabcdegh
|
| 21 |
+
ihgedcbaabcdeghi
|
| 22 |
+
jihgedcbaabcdeghij
|
| 23 |
+
kjihgedcbaabcdeghijk
|
| 24 |
+
lkjihgedcbaabcdeghijkl
|
| 25 |
+
mlkjihgedcbaabcdeghijklm
|
| 26 |
+
nmlkjihgedcbaabcdeghijklmn
|
| 27 |
+
onmlkjihgedcbaabcdeghijklmno
|
| 28 |
+
ponmlkjihgedcbaabcdeghijklmnop
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
import java.util.Random;
|
| 36 |
+
import java.util.Scanner;
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
public class Test{
|
| 41 |
+
public static void main(String[] args){
|
| 42 |
+
final Random r = new Random();
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
// round 1
|
| 46 |
+
//ADD
|
| 47 |
+
// Scanner reader= new Scanner(System.in);
|
| 48 |
+
// System.out.print("Give a lowercase letter: ");
|
| 49 |
+
// // char user_char = Character.valueOf(reader.nextLine());
|
| 50 |
+
// char user_char = reader.next().charAt(0);
|
| 51 |
+
|
| 52 |
+
// char char_a = 'a';
|
| 53 |
+
// char char_loop = 'a';
|
| 54 |
+
// String str = "";
|
| 55 |
+
// while (char_loop <= user_char) {
|
| 56 |
+
// str += char_loop;
|
| 57 |
+
// char_loop++;
|
| 58 |
+
// }
|
| 59 |
+
|
| 60 |
+
// char_loop = user_char-1;
|
| 61 |
+
// while (char_loop >= char_a) {
|
| 62 |
+
// str += char_loop;
|
| 63 |
+
// char_loop--;
|
| 64 |
+
// }
|
| 65 |
+
|
| 66 |
+
// System.out.println(str);
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
// round 2
|
| 71 |
+
// // Calculate the number of rows needed
|
| 72 |
+
// int num_rows = user_char - 'a' + 1;
|
| 73 |
+
|
| 74 |
+
// // Loop through each row
|
| 75 |
+
// for (int i = 0; i < num_rows; i++) {
|
| 76 |
+
// // Print spaces in front
|
| 77 |
+
// // eg
|
| 78 |
+
// // 'c' - 'a' = 2
|
| 79 |
+
// // num_rows = 3
|
| 80 |
+
// // BUT we want 2 spaces in front
|
| 81 |
+
// // so we do num_rows - i - 1
|
| 82 |
+
// for (int j = 0; j < num_rows - i - 1; j++) {
|
| 83 |
+
// System.out.print(" ");
|
| 84 |
+
// }
|
| 85 |
+
|
| 86 |
+
// // Print decreasing letters
|
| 87 |
+
// // row 1 - 'a'
|
| 88 |
+
// // row 2 - 'ba'
|
| 89 |
+
// // row 3 - 'cba'
|
| 90 |
+
// for (char c = 'a'+i; c >= 'a'; c--) {
|
| 91 |
+
// System.out.print(c);
|
| 92 |
+
// }
|
| 93 |
+
|
| 94 |
+
// // Print increasing letters
|
| 95 |
+
// // start from 'a', print until user_char
|
| 96 |
+
// for (char c = 'a'; c <= user_char; c++) {
|
| 97 |
+
// System.out.print(c);
|
| 98 |
+
// }
|
| 99 |
+
|
| 100 |
+
// //no need to print spaces at the back
|
| 101 |
+
// // just
|
| 102 |
+
// // Move to the next line
|
| 103 |
+
// System.out.println();
|
| 104 |
+
// }
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
// round 3
|
| 109 |
+
Scanner reader = new Scanner(System.in);
|
| 110 |
+
System.out.print("Give a lowercase letter: ");
|
| 111 |
+
// https://stackoverflow.com/questions/32798803/understanding-scanners-nextline-next-and-nextint-methods
|
| 112 |
+
// https://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner
|
| 113 |
+
//char user_char = reader.next().charAt(0);
|
| 114 |
+
char user_char = reader.findInLine(".").charAt(0);
|
| 115 |
+
|
| 116 |
+
// each row is a new line
|
| 117 |
+
// Loop through each character from 'a' to user_char
|
| 118 |
+
for (char current_char = 'a'; current_char <= user_char; current_char++) {
|
| 119 |
+
// Print leading spaces
|
| 120 |
+
// eg
|
| 121 |
+
// 'c' - 'a' = 2
|
| 122 |
+
// num_rows = 3
|
| 123 |
+
// BUT we want 2 spaces in front
|
| 124 |
+
for (int j = 0; j < (user_char - current_char); j++) {
|
| 125 |
+
System.out.print(" ");
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Print descending characters from currentChar down to 'a'
|
| 129 |
+
for (char c = current_char; c >= 'a'; c--) {
|
| 130 |
+
System.out.print(c);
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Print ascending characters from 'a' up to currentChar
|
| 134 |
+
for (char c = 'a'; c <= current_char; c++) {
|
| 135 |
+
System.out.print(c);
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// Move to the next line
|
| 139 |
+
System.out.println();
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
}
|
| 145 |
+
}
|