本文介紹了在java中制作一個(gè)倒三角形的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試使我制作的三角形面朝下.試了很多次,不知道怎么弄.
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;
}
}
}
有什么建議可以翻轉(zhuǎn)"這個(gè)三角形嗎?謝謝.
Any suggestions how I can "flip" this triangle? Thanks.
推薦答案
要翻轉(zhuǎn)三角形,你真的只需要改變迭代的方向.而不是從 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
更改為要以多少個(gè)空格和符號(hào)開頭.
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;
}
這篇關(guān)于在java中制作一個(gè)倒三角形的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!