Spaces:
Running
Running
File size: 5,632 Bytes
888e931 45588d0 888e931 45588d0 888e931 46fe522 45588d0 | 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | Write a program that asks the user to enter the lower case letter [a-z].
The program will then print a pyramid corresponding to the letter according to the example printouts shown below.
Example execution:
Give a lowercase letter: d
aa
baab
cbaabc
dcbaabcd
Another example execution:
Give a lowercase letter: p
aa
baab
cbaabc
dcbaabcd
edcbaabcde
gedcbaabcdeg
hgedcbaabcdegh
ihgedcbaabcdeghi
jihgedcbaabcdeghij
kjihgedcbaabcdeghijk
lkjihgedcbaabcdeghijkl
mlkjihgedcbaabcdeghijklm
nmlkjihgedcbaabcdeghijklmn
onmlkjihgedcbaabcdeghijklmno
ponmlkjihgedcbaabcdeghijklmnop
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
final Random r = new Random();
// round 1
//ADD
// Scanner reader= new Scanner(System.in);
// System.out.print("Give a lowercase letter: ");
// // char user_char = Character.valueOf(reader.nextLine());
// char user_char = reader.next().charAt(0);
// char char_a = 'a';
// char char_loop = 'a';
// String str = "";
// while (char_loop <= user_char) {
// str += char_loop;
// char_loop++;
// }
// char_loop = user_char-1;
// while (char_loop >= char_a) {
// str += char_loop;
// char_loop--;
// }
// System.out.println(str);
// round 2
// // Calculate the number of rows needed
// int num_rows = user_char - 'a' + 1;
// // Loop through each row
// for (int i = 0; i < num_rows; i++) {
// // Print spaces in front
// // eg
// // 'c' - 'a' = 2
// // num_rows = 3
// // BUT we want 2 spaces in front
// // so we do num_rows - i - 1
// for (int j = 0; j < num_rows - i - 1; j++) {
// System.out.print(" ");
// }
// // Print decreasing letters
// // row 1 - 'a'
// // row 2 - 'ba'
// // row 3 - 'cba'
// for (char c = 'a'+i; c >= 'a'; c--) {
// System.out.print(c);
// }
// // Print increasing letters
// // start from 'a', print until user_char
// for (char c = 'a'; c <= user_char; c++) {
// System.out.print(c);
// }
// //no need to print spaces at the back
// // just
// // Move to the next line
// System.out.println();
// }
// round 3
Scanner reader = new Scanner(System.in);
System.out.print("Give a lowercase letter: ");
// https://stackoverflow.com/questions/32798803/understanding-scanners-nextline-next-and-nextint-methods
// https://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner
// option1
//char user_char = reader.next().charAt(0);
// option2
//char user_char = reader.findInLine(".").charAt(0);
// option3 - older syntax
char user_char = reader.nextLine().charAt(0);
// each row is a new line
// Loop through each character from 'a' to user_char
for (char current_char = 'a'; current_char <= user_char; current_char++) {
// Print leading spaces
// eg
// 'c' - 'a' = 2
// num_rows = 3
// BUT we want 2 spaces in front
for (int j = 0; j < (user_char - current_char); j++) {
System.out.print(" ");
}
// Print descending characters from currentChar down to 'a'
for (char c = current_char; c >= 'a'; c--) {
System.out.print(c);
}
// Print ascending characters from 'a' up to currentChar
for (char c = 'a'; c <= current_char; c++) {
System.out.print(c);
}
// Move to the next line
System.out.println();
}
}
}
Give a lowercase letter: p
aa
baab
cbaabc
dcbaabcd
edcbaabcde
fedcbaabcdef
gfedcbaabcdefg
hgfedcbaabcdefgh
ihgfedcbaabcdefghi
jihgfedcbaabcdefghij
kjihgfedcbaabcdefghijk
lkjihgfedcbaabcdefghijkl
mlkjihgfedcbaabcdefghijklm
nmlkjihgfedcbaabcdefghijklmn
onmlkjihgfedcbaabcdefghijklmno
ponmlkjihgfedcbaabcdefghijklmnop
=== Code Execution Successful ===
Testing with input c
Give a lowercase letter: c
aa
baab
cbaabc
Testing with input g
Give a lowercase letter: g
aa
baab
cbaabc
dcbaabcd
edcbaabcde
fedcbaabcdef
gfedcbaabcdefg
Testing with input h
Give a lowercase letter: h
aa
baab
cbaabc
dcbaabcd
edcbaabcde
fedcbaabcdef
gfedcbaabcdefg
hgfedcbaabcdefgh
Testing with input r
Give a lowercase letter: r
aa
baab
cbaabc
dcbaabcd
edcbaabcde
fedcbaabcdef
gfedcbaabcdefg
hgfedcbaabcdefgh
ihgfedcbaabcdefghi
jihgfedcbaabcdefghij
kjihgfedcbaabcdefghijk
lkjihgfedcbaabcdefghijkl
mlkjihgfedcbaabcdefghijklm
nmlkjihgfedcbaabcdefghijklmn
onmlkjihgfedcbaabcdefghijklmno
ponmlkjihgfedcbaabcdefghijklmnop
qponmlkjihgfedcbaabcdefghijklmnopq
rqponmlkjihgfedcbaabcdefghijklmnopqr
|