【发布时间】:2012-11-08 01:16:06
【问题描述】:
我正在尝试反转此代码,以便可以打印带有边框的菱形。开始反转嵌套循环/一系列循环的最佳方法是什么?我尝试修改代码,但一切都变得混乱和无序。有什么建议吗?
另外,有没有办法在顶部星星的每一侧制作偶数个 .'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?
}
}
【问题讨论】:
-
+1 用于拍摄 ascii 艺术照片
-
我希望为每个 heavy 操作使用一种方法。我所说的重动作是指每个包含逻辑代码的
for-loop。一旦您设计了这些方法,就更容易反转当前代码。否则,您应该只复制并调整当前代码以具有反向打印行为。
标签: java for-loop reverse shape nested-loops