我不懂 C#,但我会用 Java 帮助你。您应该根据需要更改语法(Console.Write
== System.out.print
&& Console.Writeline
== System.out.println
)。所以这里是代码:
static void printPattern(int column){
int spaceCount = 2;//number of spaces before **, change as needed
int k;//number of times ** is printed each row, must remain always 1
for(int i = 0; i < column; i++){
System.out.println();//starts each row with a new line
for(int j = 1; j < spaceCount; j++){
System.out.print(" ");//prints j spaces in each row
}
spaceCount++;//increment spacecount each row, so j can also go + 1
for(k = 1; k <= 1; k++){
System.out.print("**");//each row prints ** k times
}
k--;//k must remain 1
}
}
对于图(3)调用printPattern(4);
图(4)printPattern(5);