我正在尝试反转此代码,以便可以打印带有边框的菱形。开始反转嵌套循环/一系列循环的最佳方法是什么?我尝试修改代码,但一切都变得混乱和无序。有什么建议吗?
另外,有没有办法在顶部星星的每一侧制作偶数个 .'s ?我能够让它工作的唯一方法是在每一面打印一个均匀的数量......
这是应该打印到控制台的内容:http: //i.imgur.com/h55r2.jpg
这是我的代码:
public class SixTester {
public static void main(String[] args)
{
int i,j,k;
int numOfRows = 8; // Made this a variable, so that the program can make any size diamond (try playing around with different values eg. 2 or 16)
// Step 1. The first Dash
for(i=0;i<numOfRows*2 +2;i++)
System.out.print(" "); // Number of spaces is double the number of rows in your 'Half Pyramid'
System.out.println("-");
// Step 2. The First half diamond
for (j=0; j<numOfRows ; j++ )
{
for(k=numOfRows*2; k>1+j*2; k--)
System.out.print(" ");
System.out.print("_/");
for (i=0; i< 2+j*4; i++)
{
// Prepare the Star rectangle, Note that it starts half the way (rows/2)
if(j >= numOfRows/2 && (i>j*2- numOfRows/2 && i<j*2+ numOfRows/2)) {
System.out.print("*");
}
else
System.out.print(".");
}
System.out.println("\\_");
}
// Next Step - Make the bottom pyramid...but how to reverse?
}
}