Java 打印由星号组成的倒三角形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19485710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:39:00  来源:igfitidea点击:

Printing an upside down triangle made of asterisks

java

提问by name123

class upsidedown {

    public static void main(String args[]) {
        int x, y;
        for (y = 1; y <= 5; y++) {
            for (x = 0; x < 5 - y; x++) {
                System.out.print(' ');
            }
            for (x = (2 - y); x < (2 - y) + (2 * y - 1); x++) {
                System.out.print('*');
            }
            System.out.print('\n');
        }
    }
}

So far my code prints out a regular, right side up triangle. How do I make it upside down?

到目前为止,我的代码打印出了一个规则的、正面朝上的三角形。我如何让它颠倒?

采纳答案by nook

Very easily. Using your same logic, just reverse the order that you print your lines with.

非常简单地。使用相同的逻辑,只需颠倒打印行的顺序即可。

public class UpsideDown {
    public static void main(String args[]) {
        int x, y;
        for (y = 5; y >= 1; y--) { //reverse here
            for (x = 0; x < 5 - y; x++)
                System.out.print(' ');
            for (x = (2 - y); x < (2 - y) + (2 * y - 1); x++)
                System.out.print('*');
            System.out.print('\n');
        }
   }
}

Also, please follow java naming conventions.

另外,请遵循java 命名约定