本文介紹了在java中制作一個倒三角形的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使我制作的三角形面朝下.試了很多次,不知道怎么弄.
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.
我知道的代碼是:
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("
");
c += 2;
}
}
}
有什么建議可以翻轉"這個三角形嗎?謝謝.
Any suggestions how I can "flip" this triangle? Thanks.
推薦答案
要翻轉三角形,你真的只需要改變迭代的方向.而不是從 i = 0
到 i <lines
你需要從 i = lines-1
到 i >= 0
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
您還需要將 c
更改為要以多少個空格和符號開頭.
You also need to change the c
to how many spaces and symbols you want to start with.
可能是這樣的:
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("
");
c -= 2;
}
這篇關于在java中制作一個倒三角形的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!