Make a upside down triangle in java(在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中制作一个倒三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在java中制作一个倒三角形


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01