这是我的返回语句代码
public static String triangle(int num) { String numline = ""; for (int x = 1;x<=num;x++) { numline+=x+" "; } return numline; }
我尝试在每次添加x时返回numline。理论上它会形成这样的三角形
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
j8ag8udp1#
您应该将numLine添加到结果中;
numLine
public static String triangle(int num) { String numline = "", triangle = ""; for (int x = 1; x <= num; x++) { numline += x + " "; triangle += numline + "\n"; } return triangle; }
和
System.out.println(triangle(5));
输出:
1条答案
按热度按时间j8ag8udp1#
您应该将
numLine
添加到结果中;和
输出: