我想颠倒这段代码,以便我可以打印带有边框的菱形图案。开始反转嵌套循环/一系列循环的最佳方法是什么?我试着对代码进行修改,但是一切都混乱无序。有什么建议?颠倒嵌套循环
另外,有没有一种方法可以在顶部星星的每一边制作偶数个。我已经能够使它发挥作用的唯一途径打印偶数每侧由一个量...
这里是应该打印到控制台: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?
}
}
+1用于拍摄ascii艺术照片 – Adam
我宁愿为每个*重*动作都采用一种方法。通过沉重的行动,我的意思是每个包含逻辑代码的“for-loop”。一旦你设计了这些方法,就可以更容易地反转当前的代码。否则,您应该复制并修改当前代码以具有反向打印行为。 –