打印菱形, 用递归该怎样写?
程序代码:
//打印菱形 import java.util.*; public class LingXing { public static void main(String[] args) { Scanner s = new Scanner(System.in); int level = s.nextInt(); print(level); } public static void print(int level) { int t = (level + 1) / 2; int i, j; for(i = 1; i<=t; i++) { //打印上半部分 for(j = 1; j<=t-i; j++) { System.out.print(" "); } for(j = 1; j<=2*i-1; j++) { System.out.print("*"); } System.out.println(); } for(i = 1; i<t; i++) { //打印下半部分 for(j = 1; j<=i; j++) { System.out.print(" "); } for(j = (t-i)*2-1; j>=1; j--) { System.out.print("*"); } System.out.println(); } } }
这是我的代码, 现在想用递归实现, 应该怎样写啊? 求指教!