Print X 打印X
Description
Enter a positive integer ‘N’. You need to return a list of strings as shown in the Example.
public class Solution {
/**
* @param n: An integer.
* @return: A string list.
*/
public List<String> printX(int n) {
// write your code here
// write your code here.
String[] result = new String[n];
for (int i = 0; i < n; i++) {
result[i] = drawChart(i, n);
}
return Arrays.asList(result);
}
private String drawChart(int i, int n) {
StringBuilder line = new StringBuilder();
for (int j = 0; j < n; j++) {
if (j == i || j == n - 1 - i) line.append('X');
else line.append(' ');
}
return line.toString();
}
}
该博客介绍了一个Java程序,用于根据输入的正整数N打印出由'X'字符组成的对称图案。程序通过一个名为`drawChart`的私有方法绘制每一行,实现了从0到N-1的索引处放置'X',其余位置填充空格,从而形成X的形状。

11万+

被折叠的 条评论
为什么被折叠?



