for循环语句
用for循环语句打印出如下图所示:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
------解决方案--------------------第一个答案自己做的:
for (int i = 1; i < 9; i = i + 2) {
for (int j = 1; j <= i; j++) {
if (j == i / 2 + 1) {
for (int j2 = i / 2 + 1; j2 > 0; j2--) {
System.out.print(j2);
}
break;
}
System.out.print(j);
}
System.out.println();
}
第二个答案:
for (int i = 1; i < 10; i = i + 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
------解决方案--------------------public class test2 {
/**
* @param args
*/
public static void f1(){
int t = 4;
for(int i = 1; i <= t; i++){
for(int j = 1; j <= i; j++){
System.out.print(j + " ");
}
for(int k = i - 1; k > 0; k--){
System.out.print(k + " ");
}
System.out.println();
}
}
public static void f2(){
int t = 4,j = 0;
for(int i = 1; i <= t; i++){
j = i*2 - 1;
for(int k = 1; k <= j; k++)
System.out.print(k + " ");
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
f1();
f2();
}
}