日期:2014-05-20 浏览次数:20864 次
for(int i = 1;i<=4;i++){ for(int j = 4;j>=1;j--){ if(j>i){ System.out.print(" "); }else{ System.out.print(i+1-j); } } for(int k = 1;k<i;k++){ System.out.print(i-k); } System.out.println(); }
------解决方案--------------------
public class OutputNum { public static void main(String[] args){ int line = 4;//输出行数(可改变line来控制输出行数) int n, d; for(int i = 1; i <= line; ++i){ n = 1; d = 1; for(int j = 0; j < line - i; ++j) System.out.print(" "); while(n > 0){ System.out.print(n + " "); if(n == i) d = -1; n += d; } System.out.println(); } } } 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1
------解决方案--------------------
public class tezx {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个数");
int num=input.nextInt();
for(int i=1;i<=num;i++){
for(int j=1;j<=num-i;j++){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print(j);
}
if(i>1){
for(int j=i-1;j>=1;j--){
System.out.print(j);
}
System.out.println();
}else{
System.out.println();
}
}
}
}
------解决方案--------------------
for example
int n = 4; for (int i=0; i<n; i++) { String format = "%" + String.format("%ds", 2*(n-i)-1); System.out.printf(format, " "); for (int j=1; j<2*(i+1); j++) { if (j <= i) { System.out.printf("%d ", j); } else { System.out.printf("%d ", 2*(i+1)-j); } } System.out.println(); }
------解决方案--------------------
public class TestPromt { public static void main(String arga[]) { int initNum = 5; for (int i = 1; i <= initNum; i++) { format(initNum ,i); generator(i); } } public static void generator(int i) { StringBuffer sb = new StringBuffer(); String myString = ""; for (int t = 1; t < i; t++) { sb.append(t).append(" "); } sb.append(i); myString = sb.toString(); sb.reverse().delete(0,String.valueOf(i).length()); System.out.println(myString + sb.toString()); } public static void format(int initNum ,int i){ for (int k = 2 * (initNum -i); k > 0; k--) System.out.print(" "); } }
------解决方案--------------------
二维数组就可以了,很简单的