通过接受字符串使用for循环打印模式(printing a pattern using for loop by accepting a string)
示例输入字符串:
BlueJ的
输出:
b***b *l*l* **u** *e*e* j***j
(其中'*'表示空格)
到目前为止我做了这么多:
int n = s.length() - 1; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= (n + 1); j++) { if (i == j || i == n - (j - 1)) System.out.print(s.charAt(i)); else { System.out.print("*"); } } System.out.println(); }但输出是:
b****b *l**l* **uu** **ee** *j**j*
Sample input string:
bluej
Output:
b***b *l*l* **u** *e*e* j***j
(where '*' indicates a blank space)
I have done this much so far:
int n = s.length() - 1; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= (n + 1); j++) { if (i == j || i == n - (j - 1)) System.out.print(s.charAt(i)); else { System.out.print("*"); } } System.out.println(); }But output is:
b****b *l**l* **uu** **ee** *j**j*
最满意答案
只需将i == n-(j-1)替换为i == n - j ,将第二个替换for (j = 0; j <= n+1; j++)循环for (j = 0; j <= n; j++) :
int n = s.length() - 1; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (i == j || i == n - j) System.out.print(s.charAt(i)); else { System.out.print("*"); } } System.out.println(); }Just replace i == n-(j-1) with i == n - j and second for loops for (j = 0; j <= n+1; j++) to for (j = 0; j <= n; j++) :
int n = s.length() - 1; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (i == j || i == n - j) System.out.print(s.charAt(i)); else { System.out.print("*"); } } System.out.println(); }更多推荐
发布评论