首页 > 解决方案 > How to return number-loops in String? [Java]

问题描述

I am practicing some exercises of java and I got some problems in returning this in String as asked in the question.

"Write nested for-loops to produce the following output from the given input n."

Can someone help me to return this same logic in String? [Java]

public String numberLoops(int n) {
    int n = 8;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < n; j++) {
            if (j < n - i) {
                System.out.print(".");
            } else if (j == j) {
                System.out.print(i);
            }

        }
        if (i < n) {
            System.out.print("");
        }
        System.out.println();
    }
    return null;
}

标签: javastringreturnnested-loops

解决方案


您可以使用对象 stringbuider 附加输出


推荐阅读