KaiquanMah commited on
Commit
78bbc3c
·
verified ·
1 Parent(s): 1c7978b

Create 12b. Letter triangle

Browse files
Week 2: Methods, strings and lists/12b. Letter triangle ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a program that asks the user to enter a string.
2
+ The program will then print a triangle of letters corresponding to the string, as shown in the sample printouts below.
3
+
4
+
5
+ Example output:
6
+ Give a string: hello
7
+ h
8
+ he
9
+ hel
10
+ hell
11
+ hello
12
+
13
+
14
+
15
+
16
+ import java.util.Random;
17
+ import java.util.Scanner;
18
+
19
+
20
+
21
+ public class Test{
22
+ public static void main(String[] args){
23
+ final Random r = new Random();
24
+
25
+
26
+ //ADD
27
+ Scanner reader= new Scanner(System.in);
28
+ System.out.print("Give a string: ");
29
+ String user_str = String.valueOf(reader.nextLine());
30
+
31
+ int wordLastIdx = user_str.length() - 1;
32
+ for (int i = 0; i <= wordLastIdx; i++) {
33
+ System.out.println(user_str.substring(0, i+1));
34
+ }
35
+
36
+
37
+ }
38
+ }
39
+
40
+
41
+
42
+
43
+
44
+
45
+ Testing with input abcdef
46
+ Give a string: abcdef
47
+ a
48
+ ab
49
+ abc
50
+ abcd
51
+ abcde
52
+ abcdef
53
+
54
+ Testing with input hi
55
+ Give a string: hi
56
+ h
57
+ hi
58
+
59
+ Testing with input xxxxx
60
+ Give a string: xxxxx
61
+ x
62
+ xx
63
+ xxx
64
+ xxxx
65
+ xxxxx
66
+
67
+ Testing with input goodevening
68
+ Give a string: goodevening
69
+ g
70
+ go
71
+ goo
72
+ good
73
+ goode
74
+ goodev
75
+ goodeve
76
+ goodeven
77
+ goodeveni
78
+ goodevenin
79
+ goodevening
80
+
81
+
82
+