Here you are:
public static void main(String[] args) {
printRhombusText("yolobird");
}
public static void printRhombusText(String s) {
// top part
for (int i = 1; i <= s.length(); ++i) {
System.out.println(s.substring(0, i));
}
// bottom part
for (int i = 1; i <= s.length(); ++i) {
// print out the space
for (int y = i; y > 0; --y) {
System.out.print(" ");
}
System.out.println(s.substring(i));
}
}
output:
y
yo
yol
yolo
yolob
yolobi
yolobir
yolobird
olobird
lobird
obird
bird
ird
rd
d
Want to add user input? Here:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
do {
System.out.print("Input your text: ");
String input = scanner.nextLine();
printRhombusText(input);
scanner.reset();
System.out.print("You want to do one more? (y/n): ");
} while (scanner.nextLine().trim().equals("y"));
}
output:
Input your text: kiet
k
ki
kie
kiet
iet
et
t
You want to do one more? (y/n): y
Input your text: ahihi
a
ah
ahi
ahih
ahihi
hihi
ihi
hi
i
You want to do one more? (y/n): n
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…