4
28/10/2024 7:08 pm
トピックスターター
Create a triangle of characters, based on input. Use java.util.Scanner and Integer.parseInt ().
Test your program with integers up to 26. Think why.
MUST LOOK LIKE THIS:


1件の回答
2
28/10/2024 7:08 pm
Here is my solution:
import java.util.Scanner;
public class PrintCharacterTriangle05 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int number = Integer.parseInt(console.nextLine());
for (int i = 1; i <= number; i++) {
for (int j = 0; j < i; j++) {
System.out.print((char) (j + 97) + " ");
}
System.out.println();
}
for (int i = number - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print((char) (j + 97) + " ");
}
System.out.println();
}
System.out.println();
}
}
