java编写数字⾦字塔_在java中的数字⾦字塔
(numberspyramidinjava)
最佳答案
英⽂原⽂
It might be a bit simpler to write and read if you make a separate method to print each line and cell and then use an if statement to determine what to print. The cell method encapsulates all the logic on what to print.
for (int i = 0; i < size; i++)
System.out.println(line(i, size));
private String line(int row, int size) {
StringBuilder line = new StringBuilder();
for (int col = 0; col < size * 2 + 1; col++) {
line.append(cell(row, col, size));
}
return line;
}
or if you are using Java 8:
return IntStream.range(0, size * 2 + 1)
.map(col -> cell(row, col, size)).collect(Collector.joining())))
Finally the method to select the character for each position:
private String cell(int row, int col, int size) {
int offset = size - col;
if (offset == row)
return "0";
else if (offset > row)
return " ";
else if (row == size - 1)
return "0";
else
return row;
}
中⽂翻译
如果您使⽤单独的⽅法打印每⼀⾏和单元格,然后使⽤if语句来确定要打印的内容,则编写和读取可能会更简单⼀些。 cell⽅法封装了要打印的内容的所有逻辑。
for(int i = 0; i&lt; size; i ++)
System.out.println(line(i,size));
private String line(int row,int size){
StringBuilder line = new StringBuilder();
for(int col = 0; col&lt; size * 2 + 1; col ++){
line.append(cell(row,col,size));
}
回归线;
}
或者如果您使⽤的是Java 8:
返回IntStream.range(0,size * 2 + 1)
.map(col - &gt; cell(row,col,size))。collect(Collector.joining())))
最后选择每个位置的字符的⽅法:
private String cell(int row,int col,int size){
int offset = size - col;
if(offset == row)
java replace方法返回" 0" ;
否则if(offset&gt; row)
返回" " ;
否则if(row == size - 1)
返回" 0" ;
其他
返回⾏;
}
It might be a bit simpler to write and read if you make a separate method to print each line and cell and then use an if statement to determine what to print. The cell method encapsulates all the logic on what to print.
for (int i = 0; i < size; i++)
System.out.println(line(i, size));
private String line(int row, int size) {
StringBuilder line = new StringBuilder();
for (int col = 0; col < size * 2 + 1; col++) {
line.append(cell(row, col, size));
}
return line;
}
or if you are using Java 8:
return IntStream.range(0, size * 2 + 1)
.map(col -> cell(row, col, size)).collect(Collector.joining())))
Finally the method to select the character for each position:
private String cell(int row, int col, int size) {
int offset = size - col;
if (offset == row)
return "0";
else if (offset > row)
return " ";
else if (row == size - 1)
return "0";
else
return row;
}
如果您使⽤单独的⽅法打印每⼀⾏和单元格,然后使⽤if语句来确定要打印的内容,则编写和读取可能会更简单⼀些。 cell⽅法封装了要打印的内容的所有逻辑。
for(int i = 0; i&lt; size; i ++)
System.out.println(line(i,size));
private String line(int row,int size){
StringBuilder line = new StringBuilder();
for(int col = 0; col&lt; size * 2 + 1; col ++){
line.append(cell(row,col,size));
}
回归线;
}
或者如果您使⽤的是Java 8:
返回IntStream.range(0,size * 2 + 1)
.map(col - &gt; cell(row,col,size))。collect(Collector.joining())))
最后选择每个位置的字符的⽅法:
private String cell(int row,int col,int size){
int offset = size - col;
if(offset == row)
返回" 0" ;
否则if(offset&gt; row)
返回" " ;
否则if(row == size - 1)
返回" 0" ;
其他
返回⾏;
}
参考答案2
Without break your existing logic, even though it is hard to read, the code can be(see the comments below): for (int i=1; i
{
for (int k=0; k < ((x-1) - i / 2); k++)
{
System.out.print(" ");
}
for (int j=0; j
{
if((j==0)||(j==i-1)){
// if it is smallest j or biggest j, print 0
System.out.print(0);
}else if(i == 2*x+1 - 2){
// if it is biggest i, print 0
System.out.print(0);
}
else{
// the rest conditions, print row number
System.out.print((i-1)/2);
}
}
System.out.println("");
}
All I did is replace System.out.print(x); with
if((j==0)||(j==i-1)){
/
/ if it is smallest j or biggest j, print 0
System.out.print(0);
}else if(i == 2*x+1 - 2){
// if it is biggest i, print 0
System.out.print(0);
}
else{
// the rest conditions, print row number System.out.print((i-1)/2);
}
参考答案3
This is my code for getting the output for(int i=0;i
{
int space=x-i;
//System.out.println("inside first for"); for(int j=0;j
{
int total_spaces=space;
if(j==0 || j==(i*2))
{
if(j==0)
{
while(total_spaces>0)
{
System.out.print(" ");
total_spaces--;
}
}
System.out.print("0");
if(j==(i*2))
{
while(total_spaces>0)
{
System.out.print(" ");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论