嗨,我编写了一个程序来为 CS 课程打印钻石;我被要求使用函数分解(我认为是全局变量?),但我不知道该怎么做;一些见解会帮助我;我的代码不是很漂亮;但它完成了这项工作。
public class SquareDiamondMid
{
public static void main(String args[]){
System.out.println("printing Diamond");
int number = BIO.getInt();
int dotsTop = (number / 2) - 1;
int count = 0;
int LineN = 1;
int DotsMiddle = 0;
int mainCount = (number / 2)+1;
int count1 = 0;
int dot = (number / 2) - 1;
while(count1<=dot){
System.out.print(".");
count1 = count1 + 1;
}
System.out.print("*");
count1 = 0;
while(count1<=dot){
System.out.print(".");
count1 = count1 + 1;
}
System.out.println("");
while(mainCount<number){
while(count<dotsTop){
System.out.print(".");
count++;
}
System.out.print("*");
while(DotsMiddle<LineN){
System.out.print(".");
DotsMiddle++;
}
System.out.print("*");
count = 0;
while(count<dotsTop){
System.out.print(".");
count++;
}
System.out.println("");
LineN = LineN + 2;
DotsMiddle = 0;
dotsTop--;
mainCount++;
count=0;
}
//bottom
int bottomLineN=1;
int numberOfBottom = (number / 2) - 2;
int bottomCount = 0;
int mainBottomCount=0;
int bottomMidCount = 0;
int bottomMidDot = number - 4;
int bottomCount2 = 0;
while(mainBottomCount<=numberOfBottom){
while(bottomCount<bottomLineN){
System.out.print(".");
bottomCount++;
}
System.out.print("*");
while(bottomMidCount<bottomMidDot){
System.out.print(".");
bottomMidCount++;
}
System.out.print("*");
while(bottomCount2<bottomLineN){
System.out.print(".");
bottomCount2++;
}
bottomCount2 = 0;
bottomMidCount = 0;
bottomMidDot = bottomMidDot - 2;
System.out.println("");
mainBottomCount++;
bottomCount=0;
bottomLineN = bottomLineN + 1;
}
int count3=0;
while(count3<=dot){
System.out.print(".");
count3 = count3 + 1;
}
System.out.print("*");
count3 = 0;
while(count3<=dot){
System.out.print(".");
count3 = count3 + 1;
}
System.out.println("");
}
提前谢谢`。