在Java中制作一个颠倒的三角形 [英] Make a upside down triangle in java
本文介绍了在Java中制作一个颠倒的三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试了很多次,但我不知道该怎么做。
我知道的代码是:
public static void drawPyramide(int lines,char symbol,boolean startDown){
// TRIANGLE
if(startDown){
//倒三角形应该在这里。
}
else {
int c = 1; (int i = 0; i for(int j = i; j System.out.print( ); (int k = 1; k< = c; k ++){
if(k%2 == 0)System.out.print();
}
else System.out.print(symbol);
}
System.out.print(\\\
);
c + = 2;
$ b $ / code>
任何建议我如何翻转这个三角形?谢谢。
解决方案
要翻转三角形,你只需要改变迭代方向。而不是从 i = 0
到 i <您需要从
i = lines-1
下降至 i> = 0
>
您还需要将 c
更改为要开始的空格和符号数。
可能看起来像这样:
int c = 2 * lines;
for(int i = lines-1; i> = 0; i--)
for(int j = i; j {
System.out.print();
for(int k = 1; k <= c; k ++)
{
if(k%2 == 0)
{
System.out.print();
}
else
{
System.out.print(symbol);
}
}
System.out.print(\\\
);
c - = 2;
}
I am trying to make the triangle I have made up side down. Tried many times, but I don't know how to do this.
The code I have know is:
public static void drawPyramide(int lines, char symbol, boolean startDown) {
//TRIANGLE
if(startDown) {
//The triangle up side down should be here.
}
else {
int c = 1;
for (int i = 0; i < lines; i++) {
for (int j = i; j < lines; j++) {
System.out.print(" ");
}
for (int k = 1; k <= c; k++) {
if (k%2==0) System.out.print(" ");
else System.out.print(symbol);
}
System.out.print("\n");
c += 2;
}
}
}
Any suggestions how I can "flip" this triangle? Thanks.
解决方案
To flip the triangle you really just need to change the direction of iteration. Instead of going from i = 0
to i < lines
you need to go down from i = lines-1
to i >= 0
You also need to change the c
to how many spaces and symbols you want to start with.
Could look like this:
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}
这篇关于在Java中制作一个颠倒的三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文