Spaces:
Running
Running
Rename Week 2: Methods, strings and lists/[GradingIssue] 22. Letter pyramid to Week 2: Methods, strings and lists/[FIXED] 22. Letter pyramid
45588d0 verified | 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 | |